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

  1. Install p-limit: npm install p-limit.
  2. Create a limiter: const pLimit = require('p-limit'); const limit = pLimit(5);.
  3. Map your tasks through the limiter: const results = await Promise.all(tasks.map(t => limit(() => t())));.
  4. Alternatively, implement a worker pool or queue pattern manually.
  5. 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.