Created
October 3, 2019 02:43
-
-
Save prof3ssorSt3v3/a70a75e65be1f65d31ef93b56c2c954d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> | |
<title>Document</title> | |
<!-- | |
Combining async await with fetch | |
--> | |
</head> | |
<body> | |
<script> | |
(async function f() { | |
let badurl = "http://"; | |
let goodurl = "http://jsonplaceholder.typicode.com/users"; | |
let url = Math.round(Math.random()) ? badurl : goodurl; | |
let data = await (await fetch(url).catch(handleErr)).json(); | |
if (data.code && data.code == 400) { | |
//problem | |
return; | |
} | |
console.log(data); | |
})(); | |
function handleErr(err) { | |
console.warn(err); | |
let resp = new Response( | |
JSON.stringify({ | |
code: 400, | |
message: "Stupid network Error" | |
}) | |
); | |
return resp; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment