What is Promise.all()?
Promise.all() is a static method in JavaScript that takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input promises have fulfilled, or rejects as soon as any of the input promises reject.
What is Promise.all()?
Promise.all() is a powerful utility for managing multiple asynchronous operations concurrently. It's particularly useful when you have several independent asynchronous tasks that need to complete before you can proceed with further logic, and the success of all tasks is critical. The method aggregates the results of all fulfilled promises into an array, maintaining the original order of the promises passed to it.
How does it work?
When you call Promise.all() with an array of promises, it creates a new promise. This new promise will:
- Fulfill if all of the promises in the iterable fulfill. The fulfillment value is an array containing the fulfillment values of the input promises, in the same order as the input promises.
- Reject as soon as any one of the promises in the iterable rejects. The rejection reason is the reason of the first promise that rejected. Subsequent rejections from other promises will be ignored.
If the iterable passed to Promise.all() contains non-promise values, these values are treated as already-fulfilled promises.
Syntax
Promise.all(iterable);
// iterable: An iterable (e.g., an Array) of Promises.
Example
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'foo');
});
Promise.all([promise1, promise2, promise3])
.then((values) => {
console.log(values);
// Expected output: Array [3, 42, 'foo']
})
.catch((error) => {
console.error(error.message);
});
const failingPromise = new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('Promise rejected!')), 50);
});
Promise.all([promise1, failingPromise, promise3])
.then((values) => {
console.log(values);
})
.catch((error) => {
console.error(error.message);
// Expected output: 'Promise rejected!' (from the failingPromise)
});
In the first example, all promises resolve successfully, and the .then() block receives an array [3, 42, 'foo']. In the second example, failingPromise rejects, causing Promise.all() to immediately reject with that promise's error, without waiting for promise3 to resolve.
Key Characteristics
- Order Preservation: The order of the results array matches the order of the input promises.
- Fail-fast Behavior: If any promise rejects,
Promise.all()immediately rejects with that promise's error. - Heterogeneous Inputs: The iterable can contain a mix of actual Promise objects and other values (which are treated as resolved promises).
- Concurrency: It's designed for running multiple asynchronous operations in parallel.
When to use Promise.all()?
- When you need to fetch data from multiple API endpoints simultaneously and process all responses together.
- When performing multiple database queries that are independent of each other.
- When a UI component needs to render only after several asynchronous data sources have loaded.
- Batch processing of files or operations that can run in parallel.