Skip to content
JavaScript·Intermediate·Coding·1 min read

What is the danger of async callbacks in forEach?

Short interview answer

forEach ignores callback return values, so it does not await the promises and the surrounding function finishes early. Use for...of for sequential work or map plus Promise.all for concurrent work.

Example

JavaScript
// Bug: "done" logs before any upload finishesfiles.forEach(async (f) => { await upload(f); });console.log('done');
// Sequential:for (const f of files) await upload(f);// Concurrent:await Promise.all(files.map(upload));

Key takeaway

Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.

← Back to Async and await

Related questions