.all()
The .all()
method returns a new Promise
object that can be accessed as an array of resolved values of fulfilled promises. It takes an iterable object, such as an Array
, that contains one or more Promise
objects. This is ideal when working with promises that depend on one another’s completion.
Syntax
Promise.all(iterableObject);
The iterableObject
is usually an array of Promise
objects. If the array is empty, a Promise
object that resolves into an empty array will be returned.
Example
Working with two promises, promiseA
and promiseB
:
const promiseA = new Promise((resolve, reject) => {resolve(23);});const promiseB = new Promise((resolve, reject) => {if (12 * 12 === 144) {resolve(144);} else {reject({errorType: 'TypeError',message: `Unexpected type - expected ${typeof (12 * 12)}.`,});}});Promise.all([promiseA, promiseB]).then((values) => {console.log(`Results from Promise.all(): [${values}]`);}).catch((err) => {console.log(`Promise.all Failed! \n${err.errorType}: ${err.message}`);}).finally(() => {console.log('Operations for Promise.all() have finished.');});
The output would be:
Results from Promise.all(): [23,144]Operations for Promise.all() have finished.
Codebyte Example
The following example demonstrates that the promise returned from Promise.all
resolves only if all promises (passed as an array) resolve. The resolved value is an array containing the values of each resolved promise.
If at least one promise was rejected, Promise.all
rejects with the value of the first rejected promise it encounters.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.