Skip to content
Latest: JSX in React explained clearly without any framework

Promise.all() – What Is the JavaScript Promise all() Method?

Promise.all() is a static method of the Promise constructor function. It returns a new promise object in a fulfilled or rejected state based on the iterable value you’ve provided as its argument.

Suppose JavaScript fulfills all the iterable argument’s values. In that case, Promise.all() will return a fulfilled promise.

However, suppose JavaScript rejects any of the iterable’s values. In that case, Promise.all() will return a rejected promise based on the reason of the first rejected value.

Syntax of the Promise.all() Static Method

Promise.all(iterable);
  • The all() method’s iterable argument’s items can be promises, thenables, or any of JavaScript’s data types.
  • Promise.all() will automatically pass each of its iterable argument’s items through Promise.resolve(). Therefore, all() will eventually resolve its iterable argument’s values to a fully-fledged promise object.

Example of Promise.all() Returning a Fulfilled Promise

Promise.all(["Orange", Promise.resolve(24), 715]);
// The invocation above will return:
Promise { <state>: "fulfilled", <value>: Array(3) ["Orange", 24, 715] }

The snippet above returned a new promise object that is fulfilled with an array of all()’s array argument because JavaScript fulfilled all the array’s values.

When Does Promise.all() Return a Fulfilled Promise?

Promise.all() returns a fulfilled promise when the static method resolves all the items in its iterable argument.

Here’s an example:

const item1 = Promise.resolve(24);
const item2 = 715;
const item3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Orange");
}, 15000);
});
Promise.all([item1, item2, item3]).then((value) => {
console.log(value);
});
// The invocation above will first return:
// Promise { <state>: "pending" }
// Then, after 15000 milliseconds (15 seconds), the invocation will return:
Array(3)[(24, 715, "Orange")];

The snippet above returned a fulfilled promise after JavaScript resolved all the array argument’s items.

Example of Promise.all() Returning a Rejected Promise

Promise.all(["Orange", Promise.reject("Pinkmelon"), Promise.reject(24), 715]);
// The invocation above will return:
Promise { <state>: "rejected", <reason>: "Pinkmelon" }

The snippet above returned a new promise object with the first rejected array item as its rejection reason.

When Does Promise.all() Return a Rejected Promise?

Promise.all() returns a rejected promise when the static method finds the first invalid item in its iterable argument.

Here’s an example:

const item1 = Promise.reject(24);
const item2 = 715;
const item3 = Promise.reject("Pinkmelon");
const item4 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Orange");
}, 15000);
});
Promise.all([item1, item2, item3, item4]).then((value) => {
console.log(value);
});
// The invocation above will return:
Promise { <state>: "rejected", <reason>: 24 }

The snippet above returned a rejected promise immediately after it found an invalid item in the array argument.