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.
xxxxxxxxxx
16
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
});
});
};
OUTPUT
Results will appear here.