-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2636-promise-pool.js
More file actions
32 lines (31 loc) · 1.35 KB
/
2636-promise-pool.js
File metadata and controls
32 lines (31 loc) · 1.35 KB
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
/**
* 2636. Promise Pool
* https://leetcode.com/problems/promise-pool/
* Difficulty: Medium
*
* Given an array of asynchronous functions functions and a pool limit n, return an asynchronous
* function promisePool. It should return a promise that resolves when all the input functions
* resolve.
*
* Pool limit is defined as the maximum number promises that can be pending at once. promisePool
* should begin execution of as many functions as possible and continue executing new functions
* when old promises resolve. promisePool should execute functions[i] then functions[i + 1] then
* functions[i + 2], etc. When the last promise resolves, promisePool should also resolve.
*
* For example, if n = 1, promisePool will execute one function at a time in series. However,
* if n = 2, it first executes two functions. When either of the two functions resolve, a 3rd
* function should be executed (if available), and so on until there are no functions left to
* execute.
*
* You can assume all functions never reject. It is acceptable for promisePool to return a promise
* that resolves any value.
*/
/**
* @param {Function[]} functions
* @param {number} n
* @return {Promise<any>}
*/
var promisePool = async function(functions, n) {
const next = () => functions[n++]?.().then(next);
return Promise.all(functions.slice(0, n).map(f => f().then(next)));
};