Last active
February 20, 2018 12:35
-
-
Save isabellachen/3fc1f6bfbad8e6c5e4452bbc79c8575e to your computer and use it in GitHub Desktop.
Async function error handling in promise chain and async fuction
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
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