Overview
Promise.all takes an iterable of promises and returns a single promise that:
- Resolves with an array of all fulfilled values (in input order) once every promise settles successfully.
- Rejects immediately with the reason of the first promise that rejects.
Implement promiseAll(promises) that behaves identically to the native Promise.all, including handling non-promise values in the input array.
Constraints
- Input is always an array (may contain promises, thenables, or plain values).
- Resolved values must preserve the original index order, not settlement order.
- Must reject with the first rejection reason — ignore later rejections.
- An empty array input should resolve immediately with
[]. - Do not use
Promise.all,Promise.allSettled, orPromise.raceinternally.
Examples
// Resolved example.
const p0 = Promise.resolve(3);
const p1 = 42;
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('foo');
}, 100);
});
await promiseAll([p0, p1, p2]); // [3, 42, 'foo']// Rejection example.
const p0 = Promise.resolve(30);
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
reject('An error occurred!');
}, 100);
});
try {
await promiseAll([p0, p1]);
} catch (err) {
console.log(err); // 'An error occurred!'
}Notes
- Non-promise values should be treated as
Promise.resolve(value). - The order of resolution does not matter — only the final array order must match input order.
- Think carefully about the counter: when do you know all promises are done?
Solution
Reveal solution
function promiseAll(promises) {
return new Promise((resolve, reject) => {
if (promises.length === 0) {
resolve([]);
return;
}
const results = new Array(promises.length);
let settled = 0;
promises.forEach((item, index) => {
Promise.resolve(item).then(
(value) => {
results[index] = value;
settled += 1;
if (settled === promises.length) {
resolve(results);
}
},
(reason) => {
reject(reason);
},
);
});
});
}Resources
promise-all-polyfill.js
Polyfill Promise.all
hardcodingJavaScriptPromises
Overview
Promise.all takes an iterable of promises and returns a single promise that:
- Resolves with an array of all fulfilled values (in input order) once every promise settles successfully.
- Rejects immediately with the reason of the first promise that rejects.
Implement promiseAll(promises) that behaves identically to the native Promise.all, including handling non-promise values in the input array.
Constraints
- Input is always an array (may contain promises, thenables, or plain values).
- Resolved values must preserve the original index order, not settlement order.
- Must reject with the first rejection reason — ignore later rejections.
- An empty array input should resolve immediately with
[]. - Do not use
Promise.all,Promise.allSettled, orPromise.raceinternally.
Examples
// Resolved example.
const p0 = Promise.resolve(3);
const p1 = 42;
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('foo');
}, 100);
});
await promiseAll([p0, p1, p2]); // [3, 42, 'foo']// Rejection example.
const p0 = Promise.resolve(30);
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
reject('An error occurred!');
}, 100);
});
try {
await promiseAll([p0, p1]);
} catch (err) {
console.log(err); // 'An error occurred!'
}Notes
- Non-promise values should be treated as
Promise.resolve(value). - The order of resolution does not matter — only the final array order must match input order.
- Think carefully about the counter: when do you know all promises are done?
Solution
Reveal solution
function promiseAll(promises) {
return new Promise((resolve, reject) => {
if (promises.length === 0) {
resolve([]);
return;
}
const results = new Array(promises.length);
let settled = 0;
promises.forEach((item, index) => {
Promise.resolve(item).then(
(value) => {
results[index] = value;
settled += 1;
if (settled === promises.length) {
resolve(results);
}
},
(reason) => {
reject(reason);
},
);
});
});
}Resources
NameTopicDifficulty
103 of 103 problems