Skip to content

Instantly share code, notes, and snippets.

@sarveshshejwadkar
Last active September 15, 2022 09:12
Show Gist options
  • Select an option

  • Save sarveshshejwadkar/21ade83c45a5590a3e1f83f7a8a32427 to your computer and use it in GitHub Desktop.

Select an option

Save sarveshshejwadkar/21ade83c45a5590a3e1f83f7a8a32427 to your computer and use it in GitHub Desktop.
not waiting async call
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