Skip to content

Instantly share code, notes, and snippets.

@tomisme
Created July 7, 2017 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomisme/feb600c03565d0b4b5a7a248f3c9ae98 to your computer and use it in GitHub Desktop.
Save tomisme/feb600c03565d0b4b5a7a248f3c9ae98 to your computer and use it in GitHub Desktop.
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function add1(x) {
var a = resolveAfter2Seconds(20);
var b = resolveAfter2Seconds(30);
return x + await a + await b;
}
add1(10).then(v => {
console.log(v); // prints 60 after 2 seconds.
});
async function add2(x) {
var a = await resolveAfter2Seconds(20);
var b = await resolveAfter2Seconds(30);
return x + a + b;
}
add2(10).then(v => {
console.log(v); // prints 60 after 4 seconds.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment