Promise.all is a method in JavaScript used to handle multiple promises concurrently. It takes an iterable (such as an array) of promises and returns a single promise. This returned promise resolves when all the promises in the iterable have resolved, or rejects if any of the promises in the iterable reject.
-
Concurrent Execution:
Promise.all
runs multiple promises in parallel.- It waits for all the promises to settle (either resolved or rejected).
-
Resolved Value:
- When all promises resolve,
Promise.all
resolves with an array of their results, maintaining the order of the original promises. - Example:
Promise.all([promise1, promise2]).then((values) => { console.log(values); });
- If
promise1
resolves tovalue1
andpromise2
resolves tovalue2
, the output will be[value1, value2]
.
- When all promises resolve,
-
Rejection Handling:
- If any promise in the iterable rejects,
Promise.all
immediately rejects with the reason of the first promise that rejected. - Example:
Promise.all([promise1, promise2]).catch((error) => { console.log(error); });
- If
promise1
rejects with anerror1
,Promise.all
will reject witherror1
.
- If any promise in the iterable rejects,
https://bigfrontend.dev/problem/implement-Promise-all
The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises
source - MDN
Could you write your own all()
? which should works the same as Promise.all()
/**
* @param {Array<any>} promises - notice input might have non-Promises
* @return {Promise<any[]>}
*/
function all(promises) {
if (promises.length === 0) {
return Promise.resolve([]);
}
const results = Array(promises.length);
let count = 0;
return new Promise((resolve, reject) => {
promises.forEach((promise, i) => {
if (!(promise instanceof Promise)) {
promise = Promise.resolve(promise);
}
promise
.then((res) => {
results[i] = res;
count++;
if (count === promises.length) {
resolve(results);
}
})
.catch((err) => {
reject(err);
});
});
});
}
const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject) => setTimeout(resolve, 100, 'foo'));
const promise3 = fetch('https://api.example.com/data');
Promise.all([promise1, promise2, promise3])
.then((values) => {
console.log(values); // [3, 'foo', Response object from fetch]
})
.catch((error) => {
console.error('One of the promises failed:', error);
});
- Aggregating Results: When you need to wait for multiple asynchronous operations to complete before proceeding, such as fetching data from multiple sources.
- Parallel Execution: When operations do not depend on each other and can be executed in parallel for efficiency.
- If the iterable passed to
Promise.all
is empty, it returns a promise that resolves to an empty array. - The order of results in the resolved array corresponds to the order of the promises passed to
Promise.all
.
By leveraging Promise.all
, developers can handle multiple asynchronous operations efficiently, ensuring that subsequent code execution waits until all specified promises have been settled.