Last active
September 15, 2022 09:12
-
-
Save sarveshshejwadkar/21ade83c45a5590a3e1f83f7a8a32427 to your computer and use it in GitHub Desktop.
not waiting async call
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() { | |
| return new Promise(resolve => { | |
| setTimeout(() => { | |
| resolve('resolved'); | |
| }, 2000); | |
| }); | |
| } | |
| async function asyncCallWithAwait() { | |
| let promiseArray = []; | |
| const startTime = performance.now(); | |
| for(let i=1;i<6;i++) { | |
| promiseArray.push(await resolveAfter2Seconds()); | |
| } | |
| const resultOfAllPromises = await Promise.all(promiseArray); | |
| console.log('Result of All Promises from asyncCallWithAwait: ', resultOfAllPromises); | |
| const endTime = performance.now(); | |
| console.log(`Call to asyncCallWithAwait took ${endTime - startTime} milliseconds`) | |
| } | |
| async function asyncCallWithoutAwait() { | |
| let promiseArray = []; | |
| const startTime = performance.now(); | |
| for(let i=1;i<6;i++) { | |
| promiseArray.push(resolveAfter2Seconds()); | |
| } | |
| const resultOfAllPromises = await Promise.all(promiseArray); | |
| console.log('Result of All Promises from asyncCallWithoutAwait: ', resultOfAllPromises); | |
| const endTime = performance.now(); | |
| console.log(`Call to asyncCallWithoutAwait took ${endTime - startTime} milliseconds`) | |
| } | |
| asyncCallWithAwait(); | |
| asyncCallWithoutAwait(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment