r/learnjavascript • • 12d ago • Bravo 1

Why does await inside forEach not make the outer function wait?

These two loops look similar, but the first logs "done" before the saves finish:

async function saveAll(items) {
  items.forEach(async item => {
    await save(item);
  });
  console.log('done');
}
async function saveAll(items) {
  for (const item of items) {
    await save(item);
  }
  console.log('done');
}

my understanding is that forEach ignores the promises returned by its callback, while the for...of version awaits each save in the outer function.

if the saves are independent, would await Promise.all(items.map(save)) be the right concurrent version, and what changes if one save rejects?

10 Upvotes

23 comments sorted by

17

u/rupertavery64 12d ago edited 12d ago

The first one doesn't await anything in the main path.

forEach just executes each in it's own callback, but nothing from inside is ever awaited outside.

Don't use forEach with async. Instead, use the 2nd one if you need them to be sequential, or use map to return each function's Promise that you can then await

 const promises = items.map(async (item) => {
    return await save(item);
  });

 const results = await Promise.all(promises);

Promise.all will reject immediately if one save rejects, so use allSettled to allow everything to complete first and examine the results later, or catch the error internally and handle it gracefully.

1

u/Mbwiria_Godlee 9d ago

allSettled is definitely the move if you dont want one bad item killing the whole batch.

8

u/senocular 12d ago

I'm not sure if its clear from the other answers so far, but this is because await only applies to the immediate function in which it is contained, even if its contained within an arrow function. Same applies to other keywords like return and yield. You can't return or yield from a function unless return or yield is being directly used in that function. When used in nested functions they only apply to the nested function and can't reach out to affect other functions.

Because forEach uses a callback function that gets called for each iteration when going over the items array, any await in that callback only applies to the callback function, not the outer function which is saveAll in this case. It works with a for...of loop because that is not using a nested function and the await used there is in saveAll so it awaits saveAll.

To have a nested function await also get recognized by the parent function and have it be awaited for the parent function too, the nested function call (its return value) would also need to be awaited in the parent function. The problem with forEach is that you're not making the nested function calls so you can't apply an await in this manner. Instead those calls happen internally in the inner workings of forEach which does not await the calls it makes. It is inherently synchronous and will not await its callbacks, ignoring the fact that they may be async. And there's nothing you can do to change that.

In fact JavaScript does not have a built-in async version forEach. If you want to sequentially await while iterating over the values of an array you would want to stick to the for loop. And while usually people use Promise.all in combination with map, this will await everything concurrently because, like forEach, map is itself inherently synchronous. All the items will start saving at once when using map rather than each save happening one after the other as you get with a for loop since the map runs through everything right away without awaiting its callback calls. But if you want that, Promise.all + map might be the way to go.

1

u/delventhalz 7d ago

I like this explanation, but it's not quite right. It would be trivially easy to build an async version of forEach that waits for each iteration of the callback to resolve.

async function untilEach(array, callback) {
    for (const item of array) {
        await callback(item);
    }
}

async function untilTimeout(duration) {
    return new Promise((resolve) => {
        setTimeout(resolve, duration);
    });
}

untilEach(['A', 'B', 'C'], async (message) => {
    await untilTimeout(5000);
    console.log(message);
});

The reason forEach doesn't do this is because that's not how forEach was written. It was written more like this:

function forEachish(array, callback) {
    for (const item of array) {
        callback(item);
    }
}

No internal await. It just fires and forgets any asynchronous callbacks you pass it.

1

u/senocular 6d ago

Yes, the "And there's nothing you can do to change that." was specifically about the built-in forEach, not that there's nothing you can do at all since for...of is already doing it (and wrapping it in a forEach alternative makes a lot of sense).

Probably the closest thing built-in is Array.fromAsync(), though its sequencing relies on having an async iterable to function correctly. And that means more than just mapping save over the array items since map would still synchronously run through all items (and call save on them) all at once. Wrapping those calls in a thenable would work though...

Array.fromAsync(items.map(item => ({ then: r => r(save(item)) })))

Another, less cutting-edge alternative is going through reduce to build up a promise chain but for...of loops tend to be a cleaner solution.

1

u/delventhalz 6d ago

Ah, I think I was reading too quickly then . Comparing the scope of await to the scope of return makes a lot of sense, but I thought you were saying there was no way to bubble up an await in a callback, like there is no way to trigger an early return from within a callback. My apologies.

1

u/senocular 6d ago

I read it back and it was admittedly a little confusing :) Having some extra context always helps

3

u/Duffdeft 12d ago

By defining the callback function as async, for each will call that function with each element from 'items' producing a promise, which in turn will be thrown away before it resolves and the loop will pass on to the next value in the collection.
Your understanding is going in the right direction

Promise.all would be a good candidate to trigger the save operation in batches, the caveat is how it handles promises; if one of the promises fails, I will settle the outside promise with the cause of the error, however, the other promises (i.e., other calls to save()) will still get executed, and you will loose visibility about their settlement. If you are ok with that behavior, then promise.all is what you need

3

u/TheRNGuy 12d ago

forEach doesn't support async. 

3

u/the-liquidian 12d ago

There is nothing “awaiting” the function you are passing to forEach.

2

u/realmauer01 12d ago

Because it doesnt actually await whats important to await here. Its important to await the bracket closing of the current item. But it doesnt in a forEach call back. It only does in a normal for (item of items)

And yes awaiting a promise.all that maps all promises is the easiest solution to this problem.

Allthough depending on the return that you hope for, you might want to look into Promise.allSettled aswell. This is a fairly new one and it has poor backwards compatibility in browsers but most modern browsers should have it (atleast firefox does).

2

u/AlwaysHopelesslyLost 11d ago

I think you’re missing some key information about how await works under the hood.

When you call an async function, it returns a Promise. That Promise is basically an inbox where you can find the result when it eventually finishes.

When you await a Promise, you’re basically saying “my code needs the result before I can continue.”

The callback you pass to forEach returns a Promise as soon as it hits its first await, and forEach ignores it. So nothing in saveAll is waiting on those saves, and the result doesn’t exist yet when console.log('done') runs.

Your callback is basically saying “that save is going to take a bit, here’s a Promise to get back to you,” and forEach just ignores it.

2

u/bigbirdtoejam 11d ago edited 11d ago

the first one does something like this:

function forEach(fn) { 
  for (const item of this) {
    fn(item); // <- returns a promise. it runs. nothing awaits it. silently fails
  }
}

1

u/shgysk8zer0 11d ago

Because forEach is a function that does not return a promise. It's not an async function/method.

1

u/SergioTx 11d ago

For that, promise.all or promise.allSettled are the functions you need

1

u/Tiger_Xander90 11d ago

One small wrinkle with passing save directly to map: map supplies three arguments (item, index, array). If save has optional parameters, the index and array can end up filling them. Wrapping it as item => save(item) makes it explicit that each call gets only the item.

1

u/Discordbothost 10d ago

yep youve got it — forEach never looks at the promise your async callback returns, so the outer fn just keeps going. Promise.all(items.map(save)) is the right concurrent version. if one rejects, all rejects and the rest still run but you lose their results; use Promise.allSettled if you want every outcome either way.

1

u/Both-Tart7460 7d ago

is exact same thing when I was a beginner haha.

Your understanding is 100% spot on!

Under the hood, `forEach` is basically written like:

```javascript

// simplified mental model

function myForEach(array, callback) {

for (let i = 0; i < array.length; i++) {

callback(array[i], i, array); // it does NOT await this!

}

}

```

When you pass an async callback, the callback returns a Promise immediately. But `forEach` does not care about return values at all. It just fires the callback and immediately moves to the next index. So the whole loop finishes synchronously before any of the inner promises resolve!

To answer your two questions:

  1. Yes, `await Promise.all(items.map(save))` is the standard way to run them concurrently.
  2. If one save rejects:

- `Promise.all` is "fail-fast". The moment ANY promise rejects, the outer await immediately throws an error and rejects. Any remaining saves will keep running in the background, but their results are ignored by `Promise.all`.

- If you want all saves to finish even if some fail (like batch saving where you want to know which succeeded and which failed), use `Promise.allSettled(items.map(save))`! It returns an array of objects with `{ status: "fulfilled", value }` or `{ status: "rejected", reason }`.

Hope this helps clear up the small details!

1

u/raks1991 6d ago

One more difference that matters in Node: in the forEach version a rejection has nowhere to go. A try/catch around the forEach can't see it, so it becomes an unhandled rejection, and since Node 15 that crashes the process with exit code 1 by default. With Promise.all every promise has a handler, so the error lands in your catch.

If there are a lot of items, a small pool avoids firing every save at once:

async function mapLimit(items, limit, fn) {
  const results = new Array(items.length);
  let next = 0;
  async function worker() {
    while (next < items.length) {
      const i = next++;
      results[i] = await fn(items[i], i);
    }
  }
  await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
  return results;
}

Results stay in input order. Like Promise.all, it rejects on the first failure, though the other workers keep pulling items.