Bounded Concurrency Promise Pool

Run an array of async tasks with a maximum concurrency, so a large batch does not open all connections at once and overwhelm a downstream service.

JavascriptResilience

Code

snippet
export async function pool(items, worker, concurrency) {
  if (concurrency < 1) throw new RangeError("concurrency must be >= 1");
  const results = new Array(items.length);
  let next = 0;
  async function run() {
    while (next < items.length) {
      const index = next++;
      results[index] = await worker(items[index], index);
    }
  }
  const runners = [];
  for (let i = 0; i < Math.min(concurrency, items.length); i++) {
    runners.push(run());
  }
  await Promise.all(runners);
  return results;
}