-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path2721-execute-asynchronous-functions-in-parallel.js
41 lines (40 loc) · 1.39 KB
/
2721-execute-asynchronous-functions-in-parallel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 2721. Execute Asynchronous Functions in Parallel
* https://leetcode.com/problems/execute-asynchronous-functions-in-parallel/
* Difficulty: Medium
*
* Given an array of asynchronous functions functions, return a new promise promise.
* Each function in the array accepts no arguments and returns a promise. All the
* promises should be executed in parallel.
*
* promise resolves:
* - When all the promises returned from functions were resolved successfully in
* parallel. The resolved value of promise should be an array of all the resolved
* values of promises in the same order as they were in the functions. The promise
* should resolve when all the asynchronous functions in the array have completed
* execution in parallel.
*
* promise rejects:
* When any of the promises returned from functions were rejected. promise should
* also reject with the reason of the first rejection.
*
* Please solve it without using the built-in Promise.all function.
*/
/**
* @param {Array<Function>} functions
* @return {Promise<any>}
*/
var promiseAll = function(functions) {
return new Promise((resolve, reject) => {
const promises = new Array(functions.length);
let count = 0;
functions.forEach((fn, i) => {
fn().then(result => {
promises[i] = result;
if (++count === promises.length) {
resolve(promises);
}
}).catch(reject);
});
});
};