Created
October 8, 2017 02:33
-
-
Save americos/6ca6d48a90c4aca1c5596d379001c116 to your computer and use it in GitHub Desktop.
Async Await Sample
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
async function getGithubUser(username) { // promise + await keyword usage allowed | |
try { // We handle async function errors with try / catch | |
const response = await fetch(`https://api.github.com/users/${username}`); // Execution stops here until fetch promise is fulfilled. | |
const user = response.json(); | |
return user; // equivalent of resolving the getGithubUser promise with user value. | |
} catch (err) { | |
throw new Error(err); // equivalent of rejecting getGithubUser promise with err value. | |
} | |
} | |
getGithubUser('americos') | |
.then(user => console.log(user)) | |
.catch(err => console.log(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment