Skip to content

Async-Pool

@avensio/async-pool schedules asynchronous tasks so that only a fixed number run at once. Use it when you need to overlap I/O-bound work—file writes, HTTP calls, database queries—without overwhelming the event loop with hundreds of pending promises. Concurrency here means multiple asynchronous operations share the same thread; the library does not add threads or worker processes and therefore does not provide parallel execution on additional CPU cores.

Install

bash
pnpm add @avensio/async-pool
# npm install @avensio/async-pool

Overview

  • API surface: a single exported factory, createPool, which validates the concurrency limit and returns a FIFO scheduler.
  • Use cases: throttling HTTP requests, batching filesystem writes, providing per-resource caps for heterogeneous workloads, and coordinating background tasks (see Usage patterns).
  • Performance guidance: choose limits from measurements of representative workloads and downstream constraints. Benchmarking and tuning advice lives in Performance.
  • Limitations: no CPU parallelism, no built-in cancellation, FIFO only—documented in Limitations.

How it works

The pool maintains a FIFO queue of task callbacks. When you invoke the pool function, the callback is either executed immediately (if the number of active tasks is below the configured limit) or enqueued. Every task completion frees a slot and triggers the next queued callback. Because the scheduler never blocks the event loop and only performs constant-time bookkeeping, the pool keeps resource usage predictable even under bursty workloads.

ts
import { createPool } from '@avensio/async-pool'

const pool = createPool(4)

await Promise.all(tasks.map((task) => pool(async () => {
  const data = await task()        // might fetch(), writeFile(), etc.
  return transform(data)
})))

Next steps

Built with VitePress – Released under the MIT License.