30
loading...
This website collects cookies to deliver better user experience
// creates a logger function to print logs with function name
function getLogger(fnName) {
return function logger(value, diffInMS) {
return console.log(
`${fnName}: Item ${value} finished waiting ${Math.round(
diffInMS / 1000
)} seconds later.`
);
};
}
// simulates an async flow, a network request for example
async function waitFor(seconds) {
// used to create the fancy waterfall
fetch("https://random-data-
api.com/api/stripe/random_stripe" + Math.random());
// the fake asynchronous task
return new Promise((resolve, reject) => {
setTimeout(resolve, seconds * 1000);
});
}
const list = [1, 2, 3, 4, 5, 6, 7, 8 , 9, 10];
export async function mainWithFor() {
const start = Date.now();
const logger = getLogger("mainWithFor");
for (const item of list) {
await waitFor(2);
const later = Date.now();
logger(item, later - start);
}
}
waitFor
to finish before proceeding to the next iteration.A good use case for this approach would be to run sequential operations, where you want the next operation to run once the former has finished.
export async function mainWithForEach() {
const start = Date.now();
const logger = getLogger("mainWithForEach");
list.forEach(async (item) => {
await waitFor(2);
const later = Date.now();
logger(item, later - start);
});
}
forEach
loop acts differently than the for
loop, while the for
loop await
the iteration before moving further, the forEach
loop executes all of the iterations simultaneously. So all the ten executions start at the same point and log after 2 seconds.A good use case for this approach would be to run parallel operations, where you don't care if the previous one finished or not. It's much faster compared to for
loop. But there is a caveat to this approach: if the api you're requesting has some sort of rate-limiting setup then making simultaneous requests can backfire.
export async function mainWithMap() {
const start = Date.now();
const logger = getLogger("mainWithMap");
const promises = list.map(async (item) => {
await waitFor(2);
const later = Date.now();
logger(item, later - start);
});
const finalAnswer = await Promise.all(promises)
}
map
function behaves exactly the same as forEach
in terms of async operations, meaning all of the callbacks start at the same time and log exactly after 2 seconds..map
returns an array of promises, (one promise per execution, in the same order). await Promise.all(promises)
to get the final answer array from it.map
should be used at places where you would need to return some data based on each async operation. If that's not the case, sticking with forEach
wouldn't be a bad choice.