Asynchronous JavaScript
Node.js Promise Concurrency Control
Firing a thousand requests at once with Promise.all will exhaust sockets, hit rate limits and may crash the process. Concurrency control processes a large list with a fixed number of operations in flight.
What is Promise Concurrency Control in Node.js?
Firing a thousand requests at once with Promise.all will exhaust sockets, hit rate limits and may crash the process. Concurrency control processes a large list with a fixed number of operations in flight.
Promise Concurrency Control example
async function mapWithLimit(items, limit, fn) {
const results = new Array(items.length);
let next = 0;
const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
while (next < items.length) {
const i = next++;
results[i] = await fn(items[i], i);
}
});
await Promise.all(workers);
return results;
}
// 5,000 items, never more than 10 requests in flight
const enriched = await mapWithLimit(jobIds, 10, id => fetchJob(id));How this works
A fixed number of workers pull from a shared cursor, so exactly `limit` operations run at any moment regardless of how many items there are.
Key points to remember
- Ten to fifty concurrent operations is a sensible starting range for HTTP work.
- Add a small delay between batches when calling a rate-limited API.
- The p-limit package does this in one line if you prefer a dependency.
Common mistakes with Promise Concurrency Control
- Promise.all over a huge array, which opens every connection at once.
- Building the entire result array in memory when streaming would do.
Node.js Promise Concurrency Control— Interview Questions & FAQs
How do I limit concurrent requests in Node.js?+
Use a worker pool that keeps a fixed number of operations in flight, or the p-limit package. Never map a large array straight into Promise.all — it starts everything simultaneously.
