Skip to content

Instantly share code, notes, and snippets.

@rajivnarayana
Created September 10, 2020 08:20
Show Gist options
  • Save rajivnarayana/ed785716fa209e15119e4d29ec7434d3 to your computer and use it in GitHub Desktop.
Save rajivnarayana/ed785716fa209e15119e4d29ec7434d3 to your computer and use it in GitHub Desktop.
A sample program that demonstrates how async await works in javascript.
//Rentrant function. - Software terminology
//generator functions. - javascript terminology ()
//Read more here https://codeburst.io/understanding-generators-in-es6-javascript-with-examples-6728834016d5
function wait() {
return new Promise(resolve => setTimeout(resolve, 3000));
}
async function sayAwaitHello() {
await wait();
console.log("Hello World");
}
async function main() {
console.log("Before function call");
await sayAwaitHello();
console.log("After function call");
}
console.log("Before main");
await main();
console.log("After main");
/**
* Actual output
* --------------
* Before main
* Before function call
* After main
* Hello World
* After function call
*/
/**
* Expected output
* ------------------
* Before main
* Before function call
* ...3 sec delay...
* Hello world
* After function call
* After main
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment