Created
September 10, 2020 08:20
-
-
Save rajivnarayana/ed785716fa209e15119e4d29ec7434d3 to your computer and use it in GitHub Desktop.
A sample program that demonstrates how async await works in javascript.
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
//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