Good evening! Here's our prompt for today.
According to the MDN docs, 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". It is useful when there is a collection of promises that you need to resolve prior to some other code execution work that neeeds to be done.
Here's an example of it being used:
1const promise1 = Promise.resolve(5);
2const promise2 = 2022;
3const promise3 = new Promise((resolve, reject) => {
4 setTimeout(() => {
5 resolve("foo");
6 }, 100);
7});
8
9Promise.all([p1, p2, p3]).then((values) => {
10 console.log(values);
11 // will print [5, 2022, "foo"]
12});
One caveat to note is that it will reject if any of the input promises are rejected. It will reject with the first rejection message or error. This is different than Promise.allSettled
. The allSettled
sibling method waits for its input promises to all complete and returns the final result of each.

Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
Promise.all = (promises) => {
return new Promise((resolve, reject) => {
const results = []; // Array to store the results of the promises
// TODO: Check if the promises array is empty, and if so, resolve with the empty results array
let pending = promises.length; // Counter for the number of pending promises
promises.forEach((promise, idx) => {
// TODO: Resolve each promise and store its result in the results array
// If all promises are resolved, call resolve with the results array
// If any promise is rejected, call reject
});
});
};
Here's our guided, illustrated walk-through.
How do I use this guide?