Implement a custom Promise.all() method
Promise.all
is a method in JavaScript that takes an iterable of promises as input and returns a single promise that resolves when all of the input promises have resolved or when any of the input promises rejects.
This returned promise resolves with an array of the results of the input promises in the same order as they were passed in.
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"]
});
Solution
- Define a function which takes an array of promises as input
- This function creates a new Promise with resolve and reject functions
- An empty array
results
will store the resolved values of each promise - Handling empty array
- Iterating through promises
- For each promise, we use
Promise.resolve(promises[i])
to ensure we handle non-promise values in the array. - For each promise, attach
then
andcatch
handlers - On success, store the result and check if all promises are resolved.
- On failure, reject the main promise.
- For each promise, we use
- Return a New Promise: The function should return a new promise that handles the logic described above.
Code
function promiseAll(promises) {
return new Promise((resolve, reject) => {
const results = [];
let pending = promises.length;
for (let i = 0; i < promises.length; i++) {
Promise.resolve(promises[i]) // Handle non-promises in the array
.then((value) => {
results[i] = value;
pending--;
if (pending === 0) {
resolve(results);
}
})
.catch((error) => {
reject(error);
});
}
if (pending === 0) {
// Handle empty array case
resolve([]);
}
});
}
// Example usage of customPromiseAll
const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve) => setTimeout(resolve, 100, "foo"));
const promise3 = new Promise((resolve) => setTimeout(resolve, 500, "bar"));
promiseAll([promise1, promise2, promise3])
.then((results) => {
console.log(results); // [3, 'foo', 'bar']
})
.catch((error) => {
console.error(error);
});