AlgoDaily Solution

1Promise.all = (promises) => {
2  return new Promise((resolve, reject) => {
3    const results = [];
4
5    if (!promises.length) {
6      resolve(results);
7      return;
8    }
9
10    let pending = promises.length;
11
12    promises.forEach((promise, idx) => {
13      Promise.resolve(promise).then((value) => {
14        results[idx] = value;
15        pending--;
16        if (pending === 0) {
17          resolve(results);
18        }
19      }, reject);
20    });
21  });
22};

Community Solutions

Community solutions are only available for premium users.

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.

JAVASCRIPT
OUTPUT
Results will appear here.