Full answer
Step by step:
- A is printed. forEach calls the async callback for each item. Every callback runs until its first await and returns a promise that forEach ignores. forEach returns immediately.
- B is printed. Then main waits 100 ms. During that time the three timers (30, 10 and 20 ms) fire in order of delay, so the output is forEach 10, forEach 20, forEach 30. forEach started all three at once, which is why the order follows the delays and not the array order.
- The for-of loop uses await inside the loop body, so each iteration waits before the next starts. The output follows array order: for-of 30, for-of 10, for-of 20. The total time is 60 ms (sequential).
- items.map(async ...) creates three promises at once, so the waits overlap (about 30 ms in total). Promise.all collects the results in the same order as the input array, not in the order of completion: [ 60, 20, 40 ].
- The last map has async functions that do not await anything. Each call returns a promise that is already resolved, and console.log shows them as Promise { 30 }, Promise { 10 }, Promise { 20 }. It does not show the plain numbers.
Lessons: use for-of when work must run in order, Promise.all with map when work can run in parallel, and never use forEach with async callbacks, because errors and completion are lost. For limited parallelism use a pool.
javascript
const wait = (ms, value) => new Promise((resolve) => setTimeout(resolve, ms, value));
async function main() {
const items = [30, 10, 20];
console.log('A');
items.forEach(async (ms) => { await wait(ms); console.log('forEach', ms); });
console.log('B');
await wait(100);
for (const ms of items) { await wait(ms); console.log('for-of', ms); }
const all = await Promise.all(items.map(async (ms) => { await wait(ms); return ms * 2; }));
console.log('all', all);
console.log(items.map(async (ms) => ms));
}
main();The snippet is the puzzle itself. The timers are 100 ms apart from the next stage, so the order is stable. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): A B forEach 10 forEach 20 forEach 30 for-of 30 for-of 10 for-of 20 all [ 60, 20, 40 ] [ Promise { 30 }, Promise { 10 }, Promise { 20 } ]
Key points
- forEach ignores the promises returned by async callbacks.
- await inside for-of makes iterations sequential.
- map with Promise.all runs in parallel and keeps input order in the result.
- An async function always returns a promise.
- Use a pool when you need parallel work with a limit.
Common mistakes
- Expecting forEach to wait before main continues.
- Thinking Promise.all returns results in the order they finish.
- Expecting console.log of async function results to show numbers.
- Using await in a loop when the calls are independent.
- Using Promise.all for thousands of items with no limit.
Follow-ups
- How do you run these tasks with at most two at a time?
- What does Promise.allSettled return?
- How does error handling differ between forEach and for-of?
Tips
- Compare the three loops in one table.
- Mention input order in Promise.all.
- Say how you would limit concurrency.