Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Implement Promise All (Main Thread)

Here is the interview question prompt, presented for reference.

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:

const promise1 = Promise.resolve(5);
const promise2 = 2022;
const promise3 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve("foo");
  }, 100);
});

Promise.all([p1, p2, p3]).then((values) => {
  console.log(values);
  // will print [5, 2022, "foo"]
});

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.

![cover](https://storage.googleapis.com/algodailyrandomassets/curriculum/frontend/interview-questions/implement-promise-all.jpg)

You can see the full challenge with visuals at this link.

Challenges • Asked almost 3 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Jun 04, 2022:

This is the main discussion thread generated for Implement Promise All (Main Thread).