2636. Promise Pool
https://leetcode.com/problems/promise-pool/
Javascript
var promisePool = async function(functions, n) {
const doNext = async () => {
if (!functions.length) {
return Promise.resolve()
}
await functions.shift()()
await doNext()
}
return Promise.all(
new Array(n)
.fill(0)
.map(doNext)
)
};
Typescript
type F = () => Promise<any>;
function promisePool(functions: F[], n: number): Promise<any> {
const doNext = async () => {
if (!functions.length) {
return Promise.resolve()
}
await functions.shift()()
await doNext()
}
return Promise.all(
new Array(n)
.fill(0)
.map(doNext)
)
};