Promise.all in JavaScript
Tags: javascript, node, webdev, advanced
Published: 2020-04-14 15:06:49

---
title: Promise.all in JavaScript
published: true
description: Promise.all is one of the powerful API in JavaScript that helps us to aggregate a group of promises. Learn Promise.all with examples.
tags: javascript, node, webdev, advanced
---
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6shxtkfnmqlh4q8hcybi.jpeg)
Promises in JavaScript are one of the powerful APIs that help us to do Async operations.

Promise.all takes Async operations to the next new level as it helps you to aggregate a group of promises.

In other words, I can say that it helps you to do concurrent operations (sometimes for free).

#### Prerequisites:

You have to know what is a **Promise** in JavaScript.

#### What is Promise.all?

Promise.all is actually a function that takes an array of promises as an input (an iterable) and returns a Promise. Then it gets resolved when all the promises get resolved or any one of them gets rejected.

For example, assume that you have ten promises (Async operation to perform a network call or a database connection). You have to know when all the promises get resolved or you have to wait till all the promises resolve. So you are passing all ten promises to Promise.all. Then, Promise.all itself as a promise will get resolved once all the ten promises get resolved or any of the ten promises get rejected with an error.

**Let’s see it in code:**

```
Promise.all([Promise1, Promise2, Promise3])
 .then(result) => {
   console.log(result)
 })
 .catch(error => console.log(`Error in promises ${error}`))
```

As you can see, we are passing an array to Promise.all. And when all three promises get resolved, Promise.all resolves and the output is consoled.

**Let’s see an example:**

{% gist https://gist.github.com/srebalaji/b4699e16767f5907d7216d26d71abf41 %}

In the above example, Promise.all resolves after 2000 ms and the output is consoled as an array.

One interesting thing about Promise.all is that the order of the promises is maintained. The first promise in the array will get resolved to the first element of the output array, the second promise will be a second element in the output array and so on.

**Let’s see another example:**

{% gist https://gist.github.com/srebalaji/7377255b0e78e86f04644a454ab33e74 %}
From the above example, it’s clear that Promise.all waits till all the promises resolve.

Let’s see what happens if any one of the promises are rejected.

{% gist https://gist.github.com/srebalaji/943465bae09f9c2a86af6fde6775840f %}

As you can see, if one of the promises fails, then all the rest of the promises fail. Then Promise.all gets rejected.

For some use cases, you don’t need that. You need to execute all the promises even if some have failed, or maybe you can handle the failed promises later.

Let’s see how to handle that.

{% gist https://gist.github.com/srebalaji/33dec6051fa03675ef91233a401a6a2f %}

#### Use cases of Promise.all

Assume that you have to perform a huge number of Async operations like sending bulk marketing emails to thousands of users.

Simple pseudo code would be:

```
for (let i=0;i<50000; i += 1) {
 sendMailForUser(user[i]) // Async operation to send a email
}
```

The above example is straightforward. But it’s not very performant. The stack will become too heavy and at one point of time, JavaScript will have a huge number of open HTTP connection which may kill the server.

A simple performant approach would be to do it in batches. Take first 500 users, trigger the mail and wait till all the HTTP connections are closed. And then take the next batch to process it and so on.

Let’s see an example:

{% gist https://gist.github.com/srebalaji/8b3ad7f068ff9771b98d903506649b19.js %}

Let’s consider another scenario: You have to build an API that gets information from multiple third-party APIs and aggregates all the responses from the APIs.

Promise.all is the perfect way of doing that. Let’s see how.

{% gist https://gist.github.com/srebalaji/d904979f74a933bac189a189a3088dae.js %}

To conclude, Promise.all is the best way to aggregate a group of promises to a single promise. This is one of the ways of achieving concurrency in JavaScript.

Hope you liked this article. :) :)