npm install @ferrow/async-batch-processorSliding-window async pool. Keeps exactly N items in flight, preserves result order, supports per-item retry with exponential backoff, abort signaling, and progress callbacks.
import { AsyncBatchProcessor } from 'async-batch-processor';
const processor = new AsyncBatchProcessor();
const items = Array(20).fill(0).map((_, i) => i);
const results = await processor.process(
items,
async (item) => {
const res = await fetch(`https://api.example.com/item/${item}`);
return res.json();
},
{
concurrency: 3,
retry: 2,
onProgress: (current, total) => console.log(`${current}/${total}`)
}
);
// results[0].item === 0, results[0].result === { ... }
// results[0].retries === 0 (or 1/2 if retried)Run worker on items with concurrency control.
Options:
concurrency(default 1) — max in-flight countretry(default 0) — max retry attempts per itemonProgress— callback(current, total)signal— abort-like { aborted: boolean }
Returns: ItemResult[] preserving input order
interface ItemResult<T, R> {
item: T;
result?: R;
error?: Error;
retries: number; // attempts made
}Shorthand for process() returning results array. Throws on error.
const results = await processor.mapLimit(items, 3, worker);Shorthand for side-effect processing.
await processor.forEachLimit(items, 3, async (item) => {
await save(item);
});- Sliding window — not chunk barriers; exactly N in flight at steady state
- No persistence — in-memory; failures not logged
- Order preserved — results array index matches items array
- Retry is exponential backoff — 10ms * 2^attempt
- AbortSignal-like only — not full AbortController
- No timeout per item — use Promise.race or external timeout
MIT
Sponsored by Ferrow
Part of the ferrow-toolkit collection · Sponsored by Ferrow