Member-only story
Stop Using forEach() for Async Operations in JavaScript
If you’ve been writing JavaScript for a while, you’ve probably written code like this:
users.forEach(async (user) => {
await sendEmail(user);
});At first glance, it looks perfectly fine.
We have async.
We have await.
The code is clean.
But there is a problem.
forEach() does not wait for your asynchronous callback to finish.
This can create some very confusing bugs, especially when you’re working with APIs, database operations, file uploads, notifications, or any other async task.
Let’s understand why this happens and what we should use instead.
The Problem With forEach() and async/await
Consider this simple example:
const users = [1, 2, 3];
users.forEach(async (user) => {
await processUser(user);
});
console.log("All users processed");You might expect the output to be:
User 1 processed
User 2 processed
User 3 processed
All users processedBut that’s not what JavaScript guarantees.









