Created
July 7, 2017 02:56
-
-
Save tomisme/feb600c03565d0b4b5a7a248f3c9ae98 to your computer and use it in GitHub Desktop.
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
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