.all()
Published Jul 27, 2021Updated Sep 3, 2021
Contribute to Docs
The .all()
method returns a new Promise that can be accessed as an array of resolved values of fulfulled 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 Promise objects, 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(): [144,42]Operations for Promise.all() have finished.
All contributors
- Anonymous contributorAnonymous contributor3077 contributions
- christian.dinh
- Anonymous contributor
- BrandonDusch
Looking to contribute?
- 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.