@upendra.manike/async-utils
Version:
Async control utilities for JavaScript/TypeScript: retry with exponential backoff, concurrency-limited queues, promise pools, sequential execution, timeout handling, abort signals. Perfect for API calls, data fetching, and async operations. AI-friendly an
41 lines (39 loc) • 2.1 kB
text/typescript
declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
type BackoffKind = 'none' | 'linear' | 'exponential';
declare function backoffDelay(attempt: number, base?: number, kind?: BackoffKind, jitter?: boolean): number;
interface RetryOptions {
retries?: number;
baseDelayMs?: number;
backoff?: BackoffKind;
retryable?: (error: unknown) => boolean;
signal?: AbortSignal;
}
declare function withRetry<T>(fn: () => Promise<T>, opts?: RetryOptions): Promise<T>;
declare function pMapSeries<I, O>(items: readonly I[], mapper: (item: I, index: number) => Promise<O>): Promise<O[]>;
declare function timeoutPromise<T>(promise: Promise<T>, ms: number, message?: string): Promise<T>;
declare function pQueue<T>(tasks: Array<() => Promise<T>>): Promise<T[]>;
declare function pMapLimit<I, O>(items: readonly I[], limit: number, mapper: (item: I, index: number) => Promise<O>): Promise<O[]>;
declare function memoizeAsync<A extends any[], R>(fn: (...args: A) => Promise<R>, keyFn?: (...args: A) => string): (...args: A) => Promise<R>;
declare function promiseRace<T>(promises: Iterable<Promise<T>>): Promise<T>;
/**
* Concurrency-limited task queue
*/
declare function concurrencyLimitedQueue<T>(tasks: Array<() => Promise<T>>, concurrency: number): Promise<T[]>;
/**
* Promise pool executor (accepts generator of tasks)
*/
declare function promisePool<T>(iterator: Iterable<() => Promise<T>>, concurrency: number): Promise<T[]>;
declare const asyncUtils: {
sleep: typeof sleep;
backoffDelay: typeof backoffDelay;
withRetry: typeof withRetry;
pMapSeries: typeof pMapSeries;
timeoutPromise: typeof timeoutPromise;
pQueue: typeof pQueue;
pMapLimit: typeof pMapLimit;
memoizeAsync: typeof memoizeAsync;
promiseRace: typeof promiseRace;
concurrencyLimitedQueue: typeof concurrencyLimitedQueue;
promisePool: typeof promisePool;
};
export { type BackoffKind, type RetryOptions, asyncUtils, backoffDelay, concurrencyLimitedQueue, memoizeAsync, pMapLimit, pMapSeries, pQueue, promisePool, promiseRace, sleep, timeoutPromise, withRetry };