Deviopath › JavaScript

Promise Pool With Concurrency Limit

Hard · Async

Write an async function promisePool(tasks, limit). Each task is a function that returns a Promise when you call it.

- Run the tasks with at most limit running at the same time.

- Start tasks in array order. As soon as one finishes, start the next waiting task (do not wait for a whole batch).

- Resolve with an array of all results in the same order as tasks (not in finishing order).

You may assume tasks never reject. The function testPool is already written: it builds tasks from { value, delay } jobs and reports results, the highest number of tasks that were running at once (peak), and the order in which jobs finished.