Node.js Concurrency Limits
Promise.all starts observing a group, but it does not impose a concurrency limit. A small worker loop can cap the number of operations in progress.
How it works
Choose a limit based on the resource being protected, such as a connection pool or a remote service’s documented allowance. Preserve input positions when callers need ordered results. This helper shares a next index on one JavaScript thread, starts at most the requested number of workers, and awaits all workers before returning. It is an instructional mapper, not a production queue with persistence, retry scheduling or cancellation propagation.
Run the code
Run this example locally with Node.js. The exact sources are reviewed on Node 22.15.1 and Node 24.22.1 unless a specialised lab states otherwise. Save the complete source as bounded-work.mjs in a separate empty exercises folder and run node bounded-work.mjs. There is no Node.js browser runner on Bhylu; the plain JavaScript playground does not provide these filesystem, server or process APIs.
Download the complete source. Ordinary Node scripts need no Apache, PHP or MySQL service. File examples own their temporary folders; network fixtures use loopback and available temporary ports.
Example
async function mapLimited(values, limit, task) {
if (!Number.isSafeInteger(limit) || limit < 1)
throw new RangeError('Positive limit required');
let next = 0;
const result = new Array(values.length);
await Promise.all(
Array.from({ length: Math.min(limit, values.length) }, async () => {
while (next < values.length) {
const index = next++;
result[index] = await task(values[index]);
}
}),
);
return result;
}
console.log((await mapLimited([3, 1, 2], 2, async (value) => value * 2)).join(','));
console.log((await mapLimited([], 2, async (value) => value)).length);
Expected result
6,2,4 0
Results keep the input order even though workers can finish at different times. The empty input starts no work. A task rejection rejects the group, but already-started siblings still need an explicit cancellation policy if they must stop.
Use the Node.js API reference for the precise version contract.