Skip to content

Commit 7d3a272

Browse files
day 19 task complete
1 parent 0052f94 commit 7d3a272

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

day19/leetcode2721.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 2721. Execute Asynchronous Functions in Parallel
2+
// URL -> https://leetcode.com/problems/execute-asynchronous-functions-in-parallel/
3+
4+
/**
5+
* @param {Array<Function>} functions
6+
* @return {Promise<any>}
7+
*/
8+
var promiseAll = function (functions) {
9+
return new Promise((resolve, reject) => {
10+
let res = [], count = 0;
11+
12+
for (let i = 0; i < functions.length; i++) {
13+
functions[i]()
14+
.then(result => {
15+
res[i] = result;
16+
count++;
17+
18+
if (count === functions.length) {
19+
resolve(res);
20+
}
21+
})
22+
.catch(error => {
23+
reject(error);
24+
});
25+
}
26+
27+
})
28+
};
29+
30+
/**
31+
* const promise = promiseAll([() => new Promise(res => res(42))])
32+
* promise.then(console.log); // [42]
33+
*/

0 commit comments

Comments
 (0)