Import
What this module is for
Usebytekit/async when you need control over timing, retries, concurrency, and flow control outside the HTTP client itself. Every export is a standalone function or class — composable and zero-dependency.
Timing
sleep
Delay execution for a given number of milliseconds. Supports an optional AbortSignal.
timeout
Add a deadline to any promise. Throws TimeoutError if the promise does not settle in time.
withTimeout
Wrap a function so every call has a built-in timeout.
Retry
retry
Retry a failed async operation with configurable backoff.
RetryOptions
Backoff strategies
Concurrency and coordination
parallel
Run async tasks with an optional concurrency limit.
ParallelOptions
sequential
Run async tasks one after another.
SequentialOptions
race
Enhanced race that resolves with the first successful promise. If every promise rejects, throws an AggregateError.
allSettled
Collect all results into fulfilled and rejected buckets with their original indices.
AllSettledResult<T>
PromisePool
A reusable, class-based concurrency controller. Unlike parallel(), PromisePool:
- Persists across multiple
run()calls. - Isolates task errors via
onError— the pool keeps running when one task fails. - Supports per-task timeouts that throw
PoolTimeoutError.
PromisePoolOptions
Error handling with onError
ApiClient integration
Pass apool option to ApiClient to rate-limit all outgoing requests:
Debounce and throttle
debounceAsync
Debounce an async function. Returns a DebouncedFunction with cancel() and flush() methods.
DebounceOptions
throttleAsync
Throttle an async function. Returns a ThrottledFunction with a cancel() method.
ThrottleOptions
Error classes
TimeoutError
Thrown by timeout() and withTimeout() when a deadline is exceeded.
AbortError
Thrown when an operation is cancelled via AbortSignal.
RetryError
Thrown when all retry attempts are exhausted.
PoolTimeoutError
Thrown by PromisePool when an individual task exceeds its configured timeout.
QueueAbortError
Thrown by RequestQueue when a queued task is cancelled — either via an external AbortSignal or the internal cancel(id) mechanism.
RequestQueue
A priority-aware, concurrency-limited task queue. At mostconcurrency tasks run simultaneously. Three priority lanes — high, normal, low — control execution order when multiple tasks are waiting.
Constructor
add(task, options?)
Enqueues a task and returns a Promise<T> that resolves/rejects with the task result.
flush()
Returns a Promise<void> that resolves when all currently queued and running tasks have settled.
State getters
Full example
RequestBatcher
Coalesces same-key HTTP requests within a time window into a single fetcher invocation. All callers sharing the same key receive the same resolved value. The deduplication key defaults to"METHOD:url:body" and is fully customisable.
Constructor
add(url, init, fetcher)
Adds a request to the current window. Returns Promise<T> resolving to the response.
flush()
Forces immediate dispatch of all pending batches. Useful in tests or when you need results without waiting for the window.
pendingCount
Number of requests waiting across all batch buckets.
Full example
ApiClient integration
Passqueue or batch options to ApiClient for transparent integration:
Combined example
All timing values are in milliseconds. Functions that accept an
AbortSignal will throw AbortError when the signal fires.