Why JavaScript forEach Doesn't Wait for Promises
A look at how async logic breaks inside forEach
Why JavaScript forEach Doesn’t Wait for Promises
Many JavaScript developers bump into a strange issue at some point. They try using forEach
with an async
function, expecting it to wait on each await
inside. Instead, it blows through the array instantly. This isn’t a bug or a quirk, it’s doing exactly what it was built to do. The way forEach
works behind the scenes just doesn’t match how asynchronous code flows. To run asynchronous logic over arrays the way you want, you need to know how these pieces fit together.
How forEach Actually Works
The way forEach
runs is clean and fast, but that speed can get in the way if you're working with anything asynchronous. You pass in a function, and it runs once per item in your array. But there’s no pause between those runs, no waiting, no promises tracked.
It Calls Your Function, Then Moves On
The forEach
method doesn’t care what your function returns. It doesn’t pause to check if it’s waiting on something. It just loops through each value and calls the function with that value. If your callback is asynchronous, forEach
still moves straight to the next one without waiting for the previous to finish.
Here’s a clear look:
This prints:
Keep reading with a 7-day free trial
Subscribe to Alexander Obregon's Substack to keep reading this post and get 7 days of free access to the full post archives.