JavaScript
Promise.all
forEach
Asynchronous Programming
Concurrency

promise.all inside a forEach loop — everything firing at once

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

When working with asynchronous JavaScript, controlling the flow of promises is a common challenge. The `Promise.all` method is one of the many utilities that allows you to handle multiple promises in parallel. However, using `Promise.all` within a `forEach` loop can lead to unexpected results because of how these constructs interact with asynchronous functions. In this article, we will explore the concept in-depth and provide technical insights and examples to clarify how it works.

Asynchronous JavaScript Overview

JavaScript is single-threaded but asynchronous, meaning it can perform time-consuming operations such as network requests or file reading without blocking the main thread. Promises represent these ongoing operations, enabling callback functions to be executed once the operation is complete.

Promises and `Promise.all`

A promise represents an operation that hasn't completed yet but is expected to in the future. The `Promise.all` method takes an iterable of promises and returns a single promise that resolves when all of the promises in the iterable have been resolved or when the iterable contains no promises. It rejects with the reason of the first promise that rejects.

`forEach` Loop in JavaScript

The `forEach` method in JavaScript is a common way to execute a function for each element in an array. It’s important to note that `forEach` itself is not made for synchronous operations, and it won't wait for a promise to resolve before moving to the next iteration.

Using `Promise.all` in a `forEach` Loop: The Pitfalls

Combining `Promise.all` with `forEach` can seem tempting because `Promise.all` ensures that all promises are resolved before executing the next operation. However, using these together can lead to misconceptions and bugs, particularly because `forEach` does not handle asynchronous operations well.

What Goes Wrong?

When you use `Promise.all` inside a `forEach` loop, you often expect the program to wait for each promise in the array to resolve before moving to the next iteration of the loop. However, this is not the case, as demonstrated by the following example:

  • If one of the promises rejects, `Promise.all` rejects immediately, effectively shortcutting further operations.
  • Use `Promise.allSettled` if you need results regardless of whether the individual promises resolve or reject.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.