Put an async callback inside forEach and the loop
finishes before any of the work does. Measured in Node 25 on 2026-09-02, three
callbacks sleeping 30, 10 and 20 milliseconds and pushing to an array: right
after the forEach, the array is []. Sixty milliseconds later it is[10,20,30], in completion order rather than in the order
written.
forEach takes a function, calls it, and throws away what it
returns. An async function always returns a promise, which is why(async () => 5)() has a constructor of Promise, so
forEach is discarding exactly the thing that would have let it wait. Nothing
warns, because discarding a return value is legal.
The two loops that do work
for (const ms of [30, 10, 20]) { await sleep(ms); } runs them one
after another and the array comes out [30,10,20], in written order,
because await inside a for..of genuinely pauses the
loop. That is the version to use when each step needs the previous one's
result.
Before writing the loop
What decides whether the calls run one after another
There is one question, it is about the data and not about performance, and getting it wrong costs either correctness or the whole wait.
- yesawait inside for..ofThe loop pauses, and the order out is the order written
- for (const ms of [30, 10, 20]) { await sleep(ms) }
- the array comes out in written order, not completion order
- noPromise.all over a mapAll start at once, and the wait is the slowest one
- map keeps the returned promise; forEach throws it away
- one rejection loses the other results, so consider allSettled
Neither arm is forEach, and that is the point: forEach with an async callback belongs to neither, because it neither waits nor collects. It is the third answer and it is always wrong.
Interactive Practice Lab
Edit and run this snippet directly in your browser (zero setup)
const scores = [78, 92, 64, 95, 88];const passed = scores.filter((n) => n >= 70);const avg = passed.reduce((a, b) => a + b, 0) / passed.length;console.log('Passed count:', passed.length);console.log('Average score:', avg.toFixed(1));Live Big-O Complexity & Binary Search Race Lab
Compare complexity curves at scale and watch Linear vs Binary Search race live.
When they are independent, sequencing is waste:await Promise.all(items.map(fn)) starts all of them and waits for
the slowest. The map matters, because unlike forEach it keeps what
the callback returned, which is the array of promises Promise.all needs. Same
callback, same loop, and the only difference is whether the return value was
kept.
What one failure does to the group
Promise.all rejects as soon as any one of them does, so three
dock lookups with the middle one failing threwdock D2 is offline and the two successful results were lost. The
other two calls were not cancelled, which is the part people misread: they ran
to completion and nobody was listening.
Handling group rejection
How one failed lookup shapes the returned result
When one of three dock requests rejects with dock D2 is offline, the choice between Promise.all and Promise.allSettled decides whether partial work survives.
- Promise.allFail-fast group rejection
- Rejects immediately on dock D2 is offline
- Two fulfilled results are discarded
- Other two calls still finish without a listener
- Promise.allSettledPer-call status array
- Waits for every call to finish
- Returns [fulfilled, rejected, fulfilled]
- Never rejects, so unchecked statuses hide failures
Three concurrent dock lookups, middle one fails
Pick Promise.all when a partial batch is unusable, and Promise.allSettled when surviving results still matter.
Promise.allSettled waits for every one and hands back a status
for each, so the same three answered["fulfilled","rejected","fulfilled"] with the failure available assettled.find(s => s.status === "rejected").reason.message. Use
all when a partial result is useless and allSettled when it is not, and be aware
that allSettled never rejects, so forgetting to check the statuses turns every
failure into silence.
What to run in 2026
Node 25 is what these were measured on, free and open source, and top level
await means a script no longer needs the async wrapper this page's examples use
inside a module. It replaced the immediately invoked async function, which was
boilerplate on every file that did anything at startup.
Runtime and lint checks
What Node 25 and ESLint block in modern async code
Top-level await removes module wrapper noise, while static linting catches dropped promises before they disappear into logs.
- Node 25 top-level await+Immediately invoked async wrapper boilerplate on startup scripts→Direct await at module scope without extra function wrappers
- ESLint no-floating-promises+Ignored promises from async forEach or unawaited calls→Promises that are awaited, returned, or handled with .catch
Together they replace runtime log hunting with clean module startup and compile-time checks on forgotten awaits.
ESLint's no-floating-promises rule is the tool that catches the whole page,
free, and it is the single most valuable rule in an async codebase: it flags a
promise nobody awaited or caught, which is what the forEach above produces. It
replaced discovering it from an unhandled rejection warning in a log, or from
data that was simply missing.
Where this stalls people
The stall is that async code fails by finishing early rather than by
erroring. A function returns, its caller carries on, and the value arrives
afterwards into an object nobody is looking at any more, so the symptom is an
empty array or an undefined field and the stack trace points at the reader
rather than the writer.
Why the symptom misleads
How an unawaited async call breaks after returning
Async bugs rarely throw at the source; instead, the caller finishes early and later reads state before the background write lands.
- Async function invokedWork starts and immediately hands back a pending promise
- Promise left unawaitedNot awaited, not returned, and missing a .catch handler
- Caller keeps runningReads an empty array or undefined field with no immediate error
- Late write lands elsewhereValue arrives into an object nobody watches, pointing traces at the reader
Tracing every async call site to confirm its promise is awaited, returned, or caught breaks this failure chain every time.
The check that finds it every time: search for async and look at
what happens to each call's return value. If a promise is produced and not
awaited, not returned and not given a .catch, that is the bug,
whether or not it is causing today's symptom. The mechanism underneath, and why
a resolved promise runs before a zero millisecond timer, is
the only language a browser
runs. Where these results usually end up is
reading and changing the
page from script, the same problem with real threads is
doing two things
at once, and the bugs only that creates, and typing the promises so a
forgotten await is a compile error is
types on top of
JavaScript, and what they catch.