@jdsalasc/solvejs-async
v1.7.1
Published
Zero-dependency JavaScript/TypeScript async utilities for production: sleep, promise timeout, retry with backoff, and concurrency-limited pMap.
Downloads
19
Maintainers
Readme
@jdsalasc/solvejs-async
Zero-dependency async/concurrency utilities for JavaScript and TypeScript.
Utilities
sleeptimeoutretrypMapdebouncePromisethrottlePromisecreateTaskQueuecreateRateLimitercreateTokenBucketLimiter
When to use this package
Use it when you need predictable retry logic, promise time limits, queues, and rate-limited async execution without heavy helper libraries.
Limitations and Constraints
throttlePromisedrops calls made during the throttle window.debouncePromisecancels previous pending calls with a rejection.
Install
npm i @jdsalasc/solvejs-asyncQuick example
import { createTaskQueue, createRateLimiter, createTokenBucketLimiter, retry, timeout, pMap } from "@jdsalasc/solvejs-async";
const data = await retry(
() => timeout(fetch("https://api.example.com/items").then((r) => r.json()), 3000),
{ retries: 2, delayMs: 200, backoffFactor: 2 }
);
const ids = await pMap(data.items, async (item) => item.id, { concurrency: 4 });
const queue = createTaskQueue({ concurrency: 2 });
const limiter = createRateLimiter({ maxCalls: 5, windowMs: 1000 });
const burstLimiter = createTokenBucketLimiter({ capacity: 10, refillTokens: 2, refillIntervalMs: 1000 });
await queue.add(() => limiter(() => fetch("https://api.example.com/reindex")));
await burstLimiter(() => fetch("https://api.example.com/heavy-sync"), 3);Endpoint tier token costs (token-bucket)
import { createTokenBucketLimiter } from "@jdsalasc/solvejs-async";
const limiter = createTokenBucketLimiter({ capacity: 20, refillTokens: 10, refillIntervalMs: 1000 });
const endpointCost = {
"/health": 1,
"/search": 2,
"/invoice/preview": 3,
"/invoice/finalize": 6,
"/batch/settlement": 10
} as const;
await limiter(() => fetch("https://api.example.com/health"), endpointCost["/health"]);
await limiter(() => fetch("https://api.example.com/batch/settlement"), endpointCost["/batch/settlement"]);