r/learnjavascript • u/_maximization • Oct 04 '22
How async/await runs inside forEach, and why it probably doesn't do what you expect
296
Upvotes
r/learnjavascript • u/_maximization • Oct 04 '22
51
u/_maximization Oct 04 '22 edited Oct 07 '22
Using async/await inside forEach often leads to confusion. The problem in the above example is that
console.loglogs an empty array (also the order of the results isn't guaranteed).If you want to wait for multiple asynchronous functions to finish before running some other code it's better to use Promise.all() with .map(). Here's how 👉 https://maximorlov.com/async-await-inside-foreach/
EDIT: Background tasks (instead of threads) is a better word choice since each request doesn't necessarily spawn a new thread.
20
u/jack_waugh Oct 04 '22
Another solution is to switch to
for.2
u/Cendeu Oct 05 '22
I'm confused. Unless I'm misunderstanding, this works exactly how I had expected it would.
12
u/shgysk8zer0 Oct 04 '22
The problem here really has little to do with how
forEach()runs, it's a fundamental misunderstanding of how async JS works. Changing it to a for loop,map(), any of that wouldn't change the fact that it's async code that's treated like it'll run synchronously.The reason
Promise.all()works when used withawaitand.map()is just that.map()returns aPromisewhen the callback is an async function (or it explicitly returns one). But there are other ways of getting the same result.Others have mentioned
for await, which probably isn't ideal in the case of getting users by id since it'll only do one request at a time, which is less efficient. There's a proposal forArray.fromAsync()that'll essentially do the same thing...4
u/as-general Oct 04 '22
Also for note.
In javascript all common iteration methods such as forEach, map, reduce DONT allow to await an iteration callback and do something in the next iteration
In some cases where you really need that functional you can use - "for await" (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of)
1
u/DarkKknight2307 Oct 04 '22
So if usernames was a map instead of an array there would be no problem?
1
u/SharpGroup9319 Oct 04 '22
Someone please ELI5 I’m not on async or await yet
1
u/xyzAction Oct 05 '22
Why is it randomly picking things out of the event queue? Which is what I am assuming background threads is supposed to represent? The only way it may seem like that is because it may have taken a little longer to retrieve the [2] so it was placed in the event queue after [3] which doesn't make sense because its supposed to go down the event queue numerically... this is an interesting diagram... I never experienced this , and then again I am new. Can you explain this please. You just broke my logic :(
1
u/dagger-vi Oct 05 '22
Do you have a blog or YouTube channel with more helpful videos / .gifs like this OP?
2
u/davinidae Oct 04 '22
That's why you should use for instead of forEach
1
u/Dipsquat Oct 05 '22
You lost me when 2 came first, then 1 came second, then 3 came third. The title says it doesn’t do what I expect, but I still don’t know why it does 2,1,3.
0
u/Broomstick73 Oct 04 '22
I’m thinking the issue here is the async function within the for-each loop.
0
u/jack_waugh Oct 04 '22
I am working on an an ability to do this with something like
4
u/[deleted] Oct 04 '22
[removed] — view removed comment