Last active
June 10, 2020 13:09
-
-
Save Verthon/e71675a4b967cbe691a102da0c79c6ab to your computer and use it in GitHub Desktop.
Async Await Fetch API 2 examples
This file contains 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
// Without try catch | |
const doFetch = async (path) => { | |
const res = await fetch('https://api.github.com/users/' + path) | |
if (res.ok) { | |
return res.json() | |
} | |
throw await res.json() | |
} | |
// With try catch | |
const doFetch = async (path) => { | |
try { | |
const res = await fetch('https://api.github.com/users/' + path) | |
if (res.ok) { | |
return res.json() | |
} | |
} catch(err) { | |
return err | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment