Wednesday, 16 June 2021

for await of vs promise.all in javascript


let i = 1;
function somethingAsync(time) {
console.log("fired");
return delay(time).then(() => Promise.resolve(i++));
}
const items = [1000, 2000, 3000, 4000];

function delay(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}

(async () => {
console.time("first way");
const promises = await Promise.all(items.map((e) => somethingAsync(e)));
for (const res of promises) {
console.log(res);
}
console.timeEnd("first way");

i = 1; //reset counter
console.time("second way");
for await (const res of items.map((e) => somethingAsync(e))) {
// do some calculations
console.log(res);
}
console.timeEnd("second way");
})();

 

No comments:

Post a Comment