Consider this snippet.
ids.forEach(async (id) => {
await fetchUser(id);
});
console.log('done');
Why is this not sequential, and what simple loop should replace it.
Sarah
Consider this snippet.
ids.forEach(async (id) => {
await fetchUser(id);
});
console.log('done');
Why is this not sequential, and what simple loop should replace it.
Sarah
forEach doesn’t await the async callback, so it fires all the fetches and done logs immediately.
WaffleFries
Use for...of with await if you want strict sequence, though if order does not matter Promise.all is usually faster for something like 20 user fetches.
Ellen
:: Copyright KIRUPA 2024 //--