Skip to content

How to Use Async Await in Array.forEach (And Why It Fails)

Array.forEach ignores async/await. Learn why it runs concurrently without waiting, and how to use for...of for sequential tasks or Promise.all() for parallel execution.

Tested With: Modern JavaScript (ES2022+), TypeScript 5.x, Node.js 18-22, Bun, Deno.

Array.prototype.forEach() does not wait for promises returned by an async callback. It fires every iteration concurrently in the background and resolves immediately.

Fast Solution (AEO / TL;DR)

// 1. SEQUENTIAL (Executes in order, one by one)
for (const item of items) {
  await processItem(item);
}

// 2. PARALLEL (Executes all items simultaneously - FASTEST)
await Promise.all(items.map(async (item) => {
  return await processItem(item);
}));

Working with streaming data or paginated APIs? Check out our deep-dive on JavaScript AsyncIterator and for await…of.

Understanding Async/Await and forEach

What is Async/Await?

Async/await is syntactic sugar built on top of JavaScript Promises. It allows you to write asynchronous code that looks and behaves like synchronous code, making it easier to understand and debug.

async function fetchData() {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    console.log(data);
}

What is forEach?

forEach is a method available on JavaScript arrays that executes a provided function once for each array element.

const numbers = [1, 2, 3];
numbers.forEach(num => console.log(num));

The Problem with Async/Await in forEach

While forEach is great for synchronous operations, it doesn’t handle asynchronous operations as you might expect. When you use await inside a forEach loop, it doesn’t wait for the asynchronous operation to complete before moving to the next iteration.

const urls = ['url1', 'url2', 'url3'];

urls.forEach(async url => {
    const response = await fetch(url);
    const data = await response.json();
    console.log(data);
});

console.log('All done?'); // This will log before the fetches complete

In the example above, console.log('All done?') will execute before the asynchronous operations inside the forEach loop complete. This is because forEach does not wait for the await expressions to resolve.

Alternatives to forEach for Async/Await

Using a for…of Loop

A for...of loop is a better alternative when you need to use async/await within a loop. Unlike forEach, for...of respects the await keyword, ensuring that each iteration waits for the asynchronous operation to complete before moving to the next one.

const urls = ['url1', 'url2', 'url3'];

async function fetchAllData() {
    for (const url of urls) {
        const response = await fetch(url);
        const data = await response.json();
        console.log(data);
    }
    console.log('All done!');
}

fetchAllData();

Using Promise.all with map

If you need to execute all asynchronous operations in parallel and wait for all of them to complete, you can use Promise.all in combination with map.

const urls = ['url1', 'url2', 'url3'];

async function fetchAllData() {
    const promises = urls.map(async url => {
        const response = await fetch(url);
        return response.json();
    });

    const results = await Promise.all(promises);
    console.log(results);
    console.log('All done!');
}

fetchAllData();

Using forEach with IIFE

If you still prefer using forEach, you can wrap the asynchronous operation in an Immediately Invoked Function Expression (IIFE) to ensure that each iteration waits for the await to resolve.

const urls = ['url1', 'url2', 'url3'];

urls.forEach(url => {
    (async () => {
        const response = await fetch(url);
        const data = await response.json();
        console.log(data);
    })();
});

console.log('All done?'); // This will still log before the fetches complete

However, note that this approach does not wait for all asynchronous operations to complete before moving to the next line of code.

Async/Await with forEach in TypeScript

TypeScript, being a superset of JavaScript, follows the same rules when it comes to async/await and forEach. The same pitfalls and solutions apply.

const urls: string[] = ['url1', 'url2', 'url3'];

urls.forEach(async (url: string) => {
    const response = await fetch(url);
    const data = await response.json();
    console.log(data);
});

console.log('All done?'); // This will log before the fetches complete

Best Practices for Using Async/Await in Loops

  1. Use for...of for Sequential Execution: If you need to execute asynchronous operations sequentially, use a for...of loop.
  2. Use Promise.all for Parallel Execution: If you need to execute all asynchronous operations in parallel, use Promise.all with map.
  3. Avoid forEach for Async Operations: While you can use forEach with IIFE, it’s generally better to use for...of or Promise.all for clarity and predictability.
  4. Handle Errors Gracefully: Always use try/catch blocks to handle errors in asynchronous operations.
async function fetchAllData() {
    try {
        for (const url of urls) {
            const response = await fetch(url);
            const data = await response.json();
            console.log(data);
        }
    } catch (error) {
        console.error('Error fetching data:', error);
    }
    console.log('All done!');
}

Conclusion

Using async/await with a forEach loop in JavaScript can be challenging due to the non-blocking nature of forEach. However, by understanding the limitations and leveraging alternatives like for...of loops and Promise.all, you can write efficient and readable asynchronous code. Whether you’re working in JavaScript or TypeScript, these best practices will help you handle asynchronous operations with confidence.

Call to Action

If you found this guide helpful, consider sharing it with your peers or leaving a comment below with your thoughts and experiences. For more in-depth tutorials and expert advice on JavaScript and TypeScript, subscribe to our newsletter and stay updated with the latest trends and best practices in web development.

Topics & Technologies

async forEach JavaScriptawait in for loopPromise.all mapTypeScript async loop

Continue Reading

View all →