R
C

AsyncPool

v0.0.9tested

A concurrency-limited async task pool with AbortSignal support. Tasks start immediately when under the concurrency limit and are queued otherwise. Each task receives the pool's AbortSignal for cooperative cancellation. Use `all()` to wait for all tasks (throws AggregateError on any failure) or `allSettled()` to inspect individual results — mirroring the Promise static API.

Example

ts
const pool = new AsyncPool({ concurrency: 3, signal: controller.signal });
for (const chunk of chunks) {
  pool.add((signal) => fetch(chunk.url, { signal }));
}
await pool.all();

Signature

ts
class AsyncPool

Properties

PropertyTypeDescription
sizereadonlynumber
activereadonlynumber
concurrencyreadonlynumber

Methods

dispose

Detaches the pool's abort listener from the provided signal. Call this when the pool is no longer needed but the signal outlives it (e.g. one long-lived controller feeding many pools), otherwise the pool stays reachable through the signal's listener list and cannot be GC'd.

ts
dispose(): void
add

Adds a task to the pool. Starts immediately if under the concurrency limit; queues otherwise. The task receives the pool's AbortSignal for cooperative cancellation.

ts
add<T>(task: (signal: AbortSignal) => Promise<T>): Promise<T>
ParameterTypeDescription
task(signal: AbortSignal) => Promise<T>
ReturnsPromise<T>
all

Like `Promise.all` — resolves when all tasks complete; throws `AggregateError` if any failed. Multiple concurrent callers share the same underlying promise.

ts
async all(): Promise<void>
ReturnsPromise<void>
allSettled

Like `Promise.allSettled` — always resolves with the settled result of every task. Multiple concurrent callers share the same underlying promise.

ts
allSettled(): Promise<Array<PromiseSettledResult<unknown>>>
ReturnsPromise<PromiseSettledResult<unknown>[]>