Skip to content

Instantly share code, notes, and snippets.

@gperrin01
Created May 22, 2017 18:55
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 gperrin01/3d79ab00a9671dafd68d6e1a63a58052 to your computer and use it in GitHub Desktop.
Save gperrin01/3d79ab00a9671dafd68d6e1a63a58052 to your computer and use it in GitHub Desktop.
// 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