Last active
November 9, 2019 07:53
-
-
Save zarcode/e72c644ea309cf8b2e989a08c3247beb to your computer and use it in GitHub Desktop.
fetch with status code handling
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
async function asyncCall() { | |
let response = await fetch("https://httpstat.us/500") | |
if (response.ok) return await response.text() | |
throw new Error(response) | |
} | |
async function callFetch() { | |
try { | |
let const = await asyncCall() | |
console.log(text) | |
} catch(e) { | |
console.log("error", e); | |
} | |
} | |
callFetch(); |
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
function syncCall() { | |
fetch("https://httpstat.us/500") | |
.then(function(response) { | |
if (!response.ok) { | |
throw Error(response); | |
} | |
return response; | |
}) | |
.then(function(response) { | |
return response.text(); | |
}) | |
.then(function(text) { | |
console.log("ok", text); | |
}) | |
.catch(function(error) { | |
console.log("error", error); | |
}) | |
} | |
syncCall(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment