Skip to content

Instantly share code, notes, and snippets.

@isabellachen
Last active February 20, 2018 12:35
Show Gist options
  • Save isabellachen/3fc1f6bfbad8e6c5e4452bbc79c8575e to your computer and use it in GitHub Desktop.
Save isabellachen/3fc1f6bfbad8e6c5e4452bbc79c8575e to your computer and use it in GitHub Desktop.
Async function error handling in promise chain and async fuction
const fetchGithubUser = async (handle) => {
const url =`https://api.github.com/users/${handle}`
const response = await fetch(url)
const body = await response.json()
if (response.status !== 200) {
throw Error(body.message)
}
return body
}
//using the promise chain
fetchGithubUser("isabellachen")
.then((res) => {
console.log(res)
})
.catch((err) => {
console.error('Error found: ', err)
})
//using an async function
const showGithubUser = async (user) => {
try {
const user = await fetchGithubUser(user)
console.log(user)
} catch (e) {
console.error('Error found: ', e)
}
}
showGithubUser("danijelmihajlovic")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment