Implement Promise.all (Medium)

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:

JAVASCRIPT
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.

Question

JAVASCRIPT
OUTPUT
Results will appear here.