Skip to content

Instantly share code, notes, and snippets.

@Verthon
Last active June 10, 2020 13:09
Show Gist options
  • Save Verthon/e71675a4b967cbe691a102da0c79c6ab to your computer and use it in GitHub Desktop.
Save Verthon/e71675a4b967cbe691a102da0c79c6ab to your computer and use it in GitHub Desktop.
Async Await Fetch API 2 examples
// 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