#Promises#Concurrency

Promise Pool With a Concurrency Limit

Run a list of async tasks with a hard limit on how many are in flight at once — the concurrency control Promise.all does not give you.

By Pratik RaiMedium

Given an array of functions that each return a promise, and a number n, run them all but never more than n at the same time.

As soon as one finishes, the next should start — do not wait for a whole batch to complete. Resolve once every function has finished.

Examples

Input:

JSfile.javascript
1const wait = (ms) => () => new Promise((r) => setTimeout(r, ms)); 2 3const t0 = Date.now(); 4await promisePool([wait(200), wait(500), wait(100), wait(250)], 2); 5Date.now() - t0;

Output:

~550

The 200ms and 500ms tasks start together. The 200ms one frees its slot first, so the 100ms task runs from 200ms to 300ms, and the 250ms task then runs until 550ms. Fixed batches of two would have taken 500 + 250 = 750ms.

Constraints

  • The list may be empty, in which case the pool resolves straight away
  • n is at least 1 and may be larger than the number of functions
  • Each function starts its work only when the pool calls it, and returns a promise

Notes

  • Fixed batches are the common wrong answer: they idle until the slowest member of each batch finishes.

How to solve it

Pooling promises

Promise.all starts everything at once. When "everything" is fifty network requests, that is a stampede — the browser queues them anyway, the server sees a spike, and the slowest response still gates the result. A pool caps how many run concurrently.

Approach

The naive fix is batching: slice the list into groups of n and await each group. It is wrong in a way that is easy to miss — a group cannot start until the slowest member of the previous one finishes, so a fast task sits idle behind a slow one.

The fix is to think in workers rather than batches. Start n of them; each pulls the next unclaimed task, awaits it, and loops. A worker that finishes early immediately takes more work.

Implementation

JSfile.javascript
1async function promisePool(functions, n) { 2 let next = 0; 3 const worker = async () => { 4 while (next < functions.length) { 5 const index = next; 6 next += 1; 7 await functions[index](); 8 } 9 }; 10 const size = Math.min(n, functions.length); 11 await Promise.all(Array.from({ length: size }, worker)); 12}

Worth knowing

A shared next counter is safe without any locking because JavaScript is single-threaded: next += 1 cannot interleave. Math.min(n, functions.length) avoids spawning workers with nothing to do.

Time is the slowest possible packing of the tasks rather than their sum, and memory holds at most n in-flight promises however long the list is.

Goal: Run every task with at most n in flight, starting the next the moment a slot frees.

Frequently asked questions

What is a promise pool in JavaScript?
A promise pool runs a list of asynchronous tasks while limiting how many are in flight at any moment. Promise.all starts everything at once; a pool starts n, and begins the next task only as a running one finishes.
Why not just split the tasks into batches?
Because a batch cannot start until the slowest member of the previous batch finishes, so quick tasks sit idle waiting on a slow neighbour. Starting n workers that each pull the next unclaimed task keeps every slot busy and finishes measurably sooner.
Is a shared counter safe without locking?
Yes. JavaScript runs your code on a single thread, so next += 1 cannot be interrupted halfway by another worker. This is one of the few places where the single-threaded model makes a concurrency problem simpler rather than harder.
Where would you use this in a real frontend?
Uploading a set of files, prefetching images, or hydrating a list where each row needs its own request. Browsers cap connections per host anyway, so firing fifty requests at once mostly builds a queue you cannot see and cannot prioritise.

Related Challenges

Continue learning with these related challenges

View All
JavaScript

Time Out an Async Function

Wrap an async function so it gives up after a deadline — the building block behind every request timeout.

JavaScript · ES6 — Pratik Rai ·

JavaScript

Promisify a Callback Function

Convert a Node-style (error, value) callback API into one that returns a promise.

JavaScript · ES6 — Pratik Rai ·

JavaScript

Resolve Promises Sequentially

Run promises one after another and collect the results in order — built with .then chaining, no async/await.

JavaScript · ES6 — Pratik Rai ·