Similar Problems
Similar Problems not available
Promise Pool - Leetcode Solution
Companies:
LeetCode: Promise Pool Leetcode Solution
Difficulty: Unknown
Topics: unknown
Problem Statement:
The problem is to write a function that takes an array of functions and returns a new function that executes the functions in parallel, with a configurable concurrency limit.
Function Signature:
The function signature is:
function promisePool(functions, limit) : Promise<void>
where:
functions
is an array of functions that can return a Promise.limit
is a number that indicates the maximum number of functions that can be executed in parallel.
The returned Promise resolves when all the functions have completed.
Example:
const fn = (time) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(time);
}, time);
});
};
const arr = [() => fn(200), () => fn(100), () => fn(300), () => fn(400), () => fn(500)];
promisePool(arr, 2).then(() => {
console.log("All the functions have completed");
});
The output of the above code will be:
All the functions have completed
The functions in the arr
array will be executed in parallel, with a concurrency limit of 2.
Detailed Solution:
To solve this problem, we will use the Promise
and async/await
features of JavaScript.
- First, we create a function
promisePool
that takes two arguments - an array of functionsfunctions
and a numberlimit
:
function promisePool(functions, limit) : Promise<void> {
// todo
}
- In the
promisePool
function, we create an arrayrunning
that will keep track of the functions that are currently running:
let running = [];
- We create a function
execute
that will be called for each function in thefunctions
array. Theexecute
function takes a functionfunc
and returns a Promise. Inside theexecute
function, we first push thefunc
to therunning
array and then execute thefunc
.
function execute(func) {
running.push(func);
return func().finally(() => {
running.splice(running.indexOf(func), 1);
});
}
Note that we use the finally
method of the Promise to remove the func
from the running
array after it is completed.
- We create a function
limitFn
that will be called for each function in thefunctions
array. ThelimitFn
function takes a functionfunc
and returns a Promise. Inside thelimitFn
function, we check if therunning
array has fewer thanlimit
elements. If there are fewer thanlimit
elements, we execute thefunc
immediately using theexecute
function. Otherwise, we wait for thePromise
of the first element in therunning
array to resolve and then execute thefunc
.
async function limitFn(func) {
while (running.length >= limit) {
await Promise.race(running);
}
return execute(func);
}
- Finally, we call the
limitFn
function for each function in thefunctions
array using themap
method of the array. Then, we wait for all the returned Promises to resolve using thePromise.all
function.
Promise.all(functions.map(limitFn));
- The complete implementation of the
promisePool
function is given below:
function promisePool(functions, limit) {
let running = [];
function execute(func) {
running.push(func);
return func().finally(() => {
running.splice(running.indexOf(func), 1);
});
}
async function limitFn(func) {
while (running.length >= limit) {
await Promise.race(running);
}
return execute(func);
}
return Promise.all(functions.map(limitFn));
}
Note:
- In the
execute
function, we usefinally
instead ofthen
to remove thefunc
from therunning
array because we want to remove thefunc
regardless of whether it is successful or not. - In the
limitFn
function, we usewhile (running.length >= limit)
instead ofif (running.length >= limit)
to ensure that we do not exceed the concurrency limit even if multiple Promises are resolved at the same time. - We can handle errors in the functions by adding a catch block inside the
execute
function.
Promise Pool Solution Code
1