Results.

SCEPTRA.TECH

JavaScript await forEach Finishes Too Soon: Choose a Loop or Promise.all

Await forEach versus sequential await loop and Promise.all completion

A script logs "finished" before its asynchronous item work is done, even though the line above starts with await. If that line calls forEach with an async callback, the missing link is usually the return value. I would inspect what the outer function is awaiting before adding a delay.

Awaiting forEach does not collect callback promises

Array.forEach calls the callback for each assigned array element, but returns undefined. It does not wait for the promises returned by async callbacks. Adding await outside it cannot turn that undefined into a promise for the whole batch.

async function run(items, work) {
  await items.forEach(async (item) => {
    await work(item);
  });
  console.log('finished'); // Not a batch-completion guarantee
}

The await inside the callback applies to that callback's execution. The caller of run is not given a connection to all those callback promises. An outer try/catch around this forEach is also not a reliable handler for later asynchronous callback rejections.

Choose sequential work when order or load matters

If one operation must finish before the next begins, put await inside an ordinary for...of loop:

async function runSequential(items, work) {
  const results = [];
  for (const item of items) {
    results.push(await work(item));
  }
  return results;
}

Here, work must return a promise that represents its completion, or a directly usable value. If it starts background work and returns early, no loop can infer the missing completion signal.

A rejected awaited operation stops this function before later iterations start, unless you deliberately catch and handle that error inside the loop. Sequential processing is also a simple way to avoid starting a large number of operations together, though a real service may need its own rate-limit and retry policy.

Use map plus Promise.all for independent work

For a small batch of independent operations that may run concurrently, build the promises and await their combined result:

async function runConcurrent(items, work) {
  return await Promise.all(
    items.map(async (item) => {
      return await work(item);
    })
  );
}

The return inside the map callback matters. With a block-bodied arrow function, forgetting to return or await the work can leave the mapped promise disconnected from that work's completion.

When all operations fulfill, Promise.all returns results in input order, not completion order. That does not force the operations themselves to finish in that order. This pattern also starts the work for the whole array without imposing a concurrency limit. Do not use it blindly for thousands of requests.

Rejection is not cancellation or rollback

Promise.all rejects when an input rejects. Other operations already started can continue. A caught error therefore does not prove that the rest stopped, that nothing was saved, or that retrying the whole batch is safe.

If you need an outcome for every input, consider Promise.allSettled and explicitly inspect each status. It waits for settlement rather than turning partial failures into success. Cancellation, retry safety and undo behavior remain separate design decisions.

What I checked locally

I ran small Node tests using timer-based mock work. Awaiting forEach reached the following log before the delayed callbacks completed. A sequential loop collected both results. A concurrent map finished its second item first, while Promise.all still returned values in input order. In a rejection test, the combined promise rejected before another operation finished, and that operation completed afterward.

These checks illustrate control flow, not network throughput or a recommended timeout. Test your own batch with delayed work and a rejected item. Verify what the returned promise covers, what can still be running after failure, and what callers should display before saying the batch is finished.

Sources

Comments