Why does an await inside array.forEach not actually wait? #48
Answered
by
Kevinchamplin
Kevinchamplin
asked this question in
Q&A
-
|
Why does an await inside array.forEach not actually wait? |
Beta Was this translation helpful? Give feedback.
Answered by
Kevinchamplin
Jun 13, 2026
Replies: 1 comment
-
|
Array.prototype.forEach ignores the promise your async callback returns, so it fires all callbacks and moves on without awaiting any of them — your code after the loop runs before the async work finishes. Use a |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Kevinchamplin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Array.prototype.forEach ignores the promise your async callback returns, so it fires all callbacks and moves on without awaiting any of them — your code after the loop runs before the async work finishes. Use a
for...ofloop with await for sequential work, orawait Promise.all(arr.map(async x => ...))to run them concurrently and wait for all.