Skip to content

.tip

ยท one of the small things

A bounded worker pool instead of Promise.all over everything

Promise.all(items.map(fetchOne)) over ten thousand items starts ten thousand requests at once, and the other end answers with 429s. Share one iterator between a fixed number of workers and only that many are ever in flight.

const queue = items.values();
await Promise.all(
  Array.from({ length: 8 }, async () => {
    for (const item of queue) await handle(item);
  }),
);

A worker takes the next item only when it is free, so one slow item holds up nothing but itself. Sharing the iterator is safe because next() finishes before another worker runs.

javascriptconcurrency