What is Promise.all() in JavaScript?

The Promise.all() method takes a list of promises as an input and returns a single promise.

The returned promise will resolve once all the promises passed as input are resolved.

When the returned promise is resolved, the resolved values of all promises are returned as an array.

Code

Let’s create two promises and pass them to the Promise.all() method. When all the promises are resolved, we will print the resolved values of all passed promises.

Console

The Promise.all() method rejects when any one of the input promises is rejected. The method will return the rejected promise with the first rejected promise’s message/error.

Console

In the above code, we have three promises. promise2 is rejected, so Promise.all() will return a rejected promise with promise2 as the message.

Free Resources