How to limit concurrent async operations in Node.js
· Category: Node.js
Short answer
Use libraries like p-limit or implement a semaphore pattern to restrict how many promises run at the same time.
Steps
- Install
p-limit:npm install p-limit. - Create a limiter:
const pLimit = require('p-limit'); const limit = pLimit(5);. - Map your tasks through the limiter:
const results = await Promise.all(tasks.map(t => limit(() => t())));. - Alternatively, implement a worker pool or queue pattern manually.
- Adjust the concurrency limit based on the downstream service capacity.
Tips
- Rate limiting and concurrency limiting are complementary; use both when calling external APIs.
- Batch processing with limited concurrency keeps memory usage stable compared to unbounded
Promise.all.
Common issues
- Setting concurrency too high can trigger rate limits or memory issues.
- Setting it too low underutilizes resources and slows down batch jobs unnecessarily.