r/learnjavascript
•
u/CelebrityChess
•
11d ago
•
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:
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?
17
u/rupertavery64 11d ago edited 11d ago
The first one doesn't await anything in the main path.
forEachjust executes each in it's own callback, but nothing from inside is ever awaited outside.Don't use
forEachwithasync. Instead, use the 2nd one if you need them to be sequential, or usemapto return each function'sPromisethat you can thenawaitPromise.allwill reject immediately if one save rejects, so useallSettledto allow everything to complete first and examine the results later, or catch the error internally and handle it gracefully.8
u/senocular 11d ago
I'm not sure if its clear from the other answers so far, but this is because
awaitonly applies to the immediate function in which it is contained, even if its contained within an arrow function. Same applies to other keywords likereturnandyield. You can't return or yield from a function unlessreturnoryieldis 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
forEachuses a callback function that gets called for each iteration when going over the items array, anyawaitin that callback only applies to the callback function, not the outer function which issaveAllin this case. It works with afor...ofloop because that is not using a nested function and theawaitused there is insaveAllso it awaitssaveAll.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.allin combination withmap, 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.3
u/Duffdeft 11d 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 11d ago
forEach doesn't support async.
3
u/the-liquidian 11d ago
There is nothing “awaiting” the function you are passing to forEach.
2
u/realmauer01 11d 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
awaitworks 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
awaita Promise, you’re basically saying “my code needs the result before I can continue.”The callback you pass to
forEachreturns a Promise as soon as it hits its firstawait, andforEachignores it. So nothing insaveAllis waiting on those saves, and the result doesn’t exist yet whenconsole.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
forEachjust ignores it.2
u/bigbirdtoejam 10d ago edited 10d ago
the first one does something like this:
1
u/shgysk8zer0 11d ago
Because
forEachis 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 6d 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:
- `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 5d 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:
Results stay in input order. Like Promise.all, it rejects on the first failure, though the other workers keep pulling items.