Sitemap

Member-only story

Stop Using forEach() for Async Operations in JavaScript

6 min readOct 6, 2026

--

Press enter or click to view image in full size

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 processed

But that’s not what JavaScript guarantees.

--

--

DevLogic - Engineering Thinking
DevLogic - Engineering Thinking

Written by DevLogic - Engineering Thinking

Java Full Stack Developer | Java, Spring Boot, Microservices, Kafka & Angular | Writing about backend engineering, system design & real-world development.