Skip to content

Instantly share code, notes, and snippets.

@bmorelli25
Created October 30, 2017 14:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmorelli25/cad2e4db091a5197e165624b2b2628dc to your computer and use it in GitHub Desktop.
Save bmorelli25/cad2e4db091a5197e165624b2b2628dc to your computer and use it in GitHub Desktop.
Async/Await Example
function doubleAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x * 2);
}, 2000);
});
}
async function addAsync(x) {
const a = await doubleAfter2Seconds(10);
const b = await doubleAfter2Seconds(20);
const c = await doubleAfter2Seconds(30);
return x + a + b + c;
}
addAsync(10).then((sum) => {
console.log(sum);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment