Skip to content

Instantly share code, notes, and snippets.

@americos
Created October 8, 2017 02:33
Show Gist options
  • Save americos/6ca6d48a90c4aca1c5596d379001c116 to your computer and use it in GitHub Desktop.
Save americos/6ca6d48a90c4aca1c5596d379001c116 to your computer and use it in GitHub Desktop.
Async Await Sample
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