Loading problem…
Implement a function that limits the number of concurrent promise executions. Given an array of promise-returning functions and a limit k, execute at most k promises in parallel.
You are given an array of functions that return promises, and a limit k. Your task is to implement a function that:
k promises concurrentlyk (number of concurrent executions)k promises at a timeconst tasks = [
() => new Promise(resolve => setTimeout(() => resolve(1), 100)),
() => new Promise(resolve => setTimeout(() => resolve(2), 200)),
() => new Promise(resolve => setTimeout(() => resolve(3), 300)),
() => new Promise(resolve => setTimeout(() => resolve(4), 400)),
];
// Run at most 2 tasks in parallel
promiseLimit(tasks, 2).then(results => {
console.log(results); // [1, 2, 3, 4] (in order)
});This problem models real concurrency control: