Bounded Concurrency for JavaScript Async Work Queues
Run independent requests in parallel without flooding a service or allocating an unbounded number of pending tasks.
Bounded concurrency processes a collection of independent tasks with a fixed number of active workers. It offers more throughput than fully sequential execution while avoiding the burst and resource use of starting every request at once.
THE PRODUCTION PROBLEM STATEMENT
Promise.all over a large array of started requests does not limit concurrency: every operation begins immediately. A bulk import can overload a service, saturate browser connections, or consume too much memory before the first result is handled.
Separate task creation from task execution
A limiter should accept functions that start work, rather than promises that have already started. It can then decide when each task gets a slot and keep the number of active operations below a known bound.
For independent jobs, results can be stored by input index so completion order does not change output order. Decide whether one failure should stop the queue or be recorded while remaining work continues.
async function mapLimited(items, limit, task) {
const results = new Array(items.length);
let next = 0;
async function worker() {
while (true) {
const index = next++;
if (index >= items.length) return;
results[index] = await task(items[index], index);
}
}
await Promise.all(Array.from(
{ length: Math.min(limit, items.length) }, worker
));
return results;
}Choose failure and cancellation semantics explicitly
Promise.all rejects when a worker rejects, but work already in progress does not automatically stop. If later tasks must not start after a failure, add a shared stop flag and abort signal, and define how completed results are reported.
For jobs that should all finish independently, capture each outcome instead of throwing from the worker. This resembles allSettled semantics while still controlling the number of active operations.
Tune the limit to the constrained resource
A useful limit depends on the operation: remote API quotas, database pool size, CPU capacity, and memory have different constraints. Keep the limit configurable and instrument queue wait time, active tasks, and failures.
Bounded concurrency limits active work, but it does not necessarily bound the pending queue. For an unbounded producer, use a queue with a maximum capacity and a policy for pausing or rejecting new work.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
No. If its inputs are promises already created by starting work, all those operations may be active at once.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Resilient Full-Stack Architecture: Idempotent APIs, Optimistic UI, and Cache Invalidation
Engineering robust distributed state between client and server: idempotency keys, safe optimistic rollbacks, exponential backoff with jitter, and cache coherence.
The JavaScript Event Loop in Depth: Microtasks, Macrotasks, and Browser Frame Budgeting
An exhaustive dissection of V8 execution contexts, microtask queue drainage, task scheduling, requestAnimationFrame, and preventing main-thread UI jank.