Created
May 22, 2017 18:55
-
-
Save gperrin01/3d79ab00a9671dafd68d6e1a63a58052 to your computer and use it in GitHub Desktop.
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
// Declare the async function | |
async function addInFourSeconds(x) { | |
// In 2 seconds, this Promise will be resolved with the value 10 | |
// Nothing else will be executed until these 2 seconds are over | |
var y = await resolveAfterTwoSeconds(10); | |
// print 10, since the above Promise has been resolved | |
console.log(y); | |
// In 2 seconds, this Promise will be resolved with the value 10 | |
// Nothing else will be executed until these 2 seconds are over | |
var z = await resolveAfterTwoSeconds(20); | |
// print 20, since the above Promise has been resolved | |
console.log(z); | |
// At this point, y=10, z=10, and both are ready to be used | |
using .then() | |
return x + y + z; | |
// Run the above function using 5 as argument | |
addInFourSeconds(5).then(value => { | |
// this is the above “x+y+z”, equal to 5+10+20 = 35 | |
console.log(value); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment