@sejinjja/promise-pool-kit
v0.1.78
Published
A tiny TypeScript toolkit for retry, timeout, and controlled concurrency in async workloads.
Maintainers
Readme
@sejinjja/promise-pool-kit
Lightweight TypeScript utilities for async workloads:
- retry with exponential backoff and jitter
- task-level timeout
- concurrency-limited promise pool
- abort signal support
Installation
npm i @sejinjja/promise-pool-kitQuick Start
import { runPool } from "@sejinjja/promise-pool-kit";
const urls = [
"https://api.example.com/a",
"https://api.example.com/b",
"https://api.example.com/c"
];
const result = await runPool(
urls,
async (url, { signal }) => {
const response = await fetch(url, { signal });
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
return response.json();
},
{
concurrency: 4,
timeoutMs: 5000,
retry: {
retries: 2,
minDelayMs: 200,
maxDelayMs: 2000,
jitter: "full"
}
}
);
if (result.hasErrors) {
console.error(result.rejected);
} else {
console.log(result.fulfilled.map((item) => item.value));
}API
retry(operation, options?)
Retry a single async operation.
import { retry } from "@sejinjja/promise-pool-kit";
const token = await retry(
async (attempt) => {
const response = await fetch("https://example.com/token");
if (!response.ok) throw new Error(`Failed attempt ${attempt}`);
return response.text();
},
{
retries: 3,
minDelayMs: 100,
maxDelayMs: 2000,
jitter: "full",
timeoutMs: 3000
}
);runPool(inputs, worker, options?)
Run tasks with controlled concurrency.
concurrencydefault:5stopOnErrordefault:falsetimeoutMsapplies per task attemptretryapplies per taskonProgressreceives current summary and last settled task
assertPoolSuccess(result)
Throw AggregateError when runPool contains rejected tasks.
Error Types
PoolAbortedErrorTaskTimeoutError
Local Development
npm install
npm run checknpm run check also validates CHANGELOG.md heading format, version ordering, release-date validity, section bullet presence, package-lock.json metadata consistency, npm pack --dry-run file list expectations, and main/module/types/exports metadata consistency.
Open Source Workflow
- Open an issue or discussion.
- Create a branch and add tests for behavior changes.
- Submit a pull request using the template.
License
MIT
