Skip to content

Namespace Async

Async utilities for timeouts, abortable operations, automatic retry, structured concurrency, and background task management.

Install

Use this namespace only:

bash
npm i @litert/utils-async

Or install the full bundle:

bash
npm i @litert/utils

And then import from it.

ts
import * as LibUtils from '@litert/utils';
LibUtils.Async.autoRetry(...);

// or
import { Async as LibAsync } from '@litert/utils';
LibAsync.autoRetry(...);

Exports

Classes

NameDescription
AbortTimeoutControllerAn AbortController that automatically fires after a configurable timeout.
PromiseController<T>Decouples a Promise's resolve/reject functions from the promise itself.
FiberController<T>Controls an async fiber lifecycle: run, sleep, resume, and abort.
BackgroundRunnerRuns async callbacks in the background without blocking the calling fiber.

Functions

NameDescription
sleepDelays for the given number of milliseconds, optionally abortable.
withTimeoutWraps a Promise or async function with a timeout.
withAbortSignalWraps a Promise or async function with an AbortSignal.
autoRetryAutomatically retries a failing async function with configurable backoff.
createExponentialBackoffDelayGeneratorCreates an exponential backoff delay generator.
fullJitterApplies a full-jitter effect to a retry delay.
equalJitterApplies an equal-jitter effect to a retry delay.
compositeRetryDelayGeneratorComposes a delay generator with jitter and a maximum delay cap.

Constants

NameDescription
DEFAULT_BG_RUNNER_WAIT_FNDefault wait function used by BackgroundRunner.runLater.
DEFAULT_BEFORE_RETRYDefault beforeRetry callback using exponential backoff with full jitter.

Errors

NameDescription
TimeoutErrorThrown when an async operation exceeds the configured timeout.
AbortedErrorThrown when an async operation is aborted via an AbortSignal.

Typings

NameDescription
IPromiseResolver<T>Function type for a Promise resolver.
IPromiseRejecter<T>Function type for a Promise rejecter.
IWithTimeoutOptions<T>Options for withTimeout.
IWithAbortSignalOptions<T>Options for withAbortSignal.
IRetryOptions<T>Options for autoRetry.
IRetryContextContext passed to each retry attempt.
IBeforeRetryCallbackType for the beforeRetry callback.
IRetryDelayGeneratorType for a retry delay generator function.
IRetryDelayJitterFunctionType for a jitter function applied to retry delays.
IRetryDelayOptionsOptions for compositeRetryDelayGenerator.
IFiberContext<T>Context passed to a fiber execution function.
IFiberExecution<T>Fiber execution function type.
IFiberOptions<T>Constructor options for FiberController.
IBackgroundRunnerOptionsConstructor options for BackgroundRunner.
IBackgroundRunnerEventsEvents emitted by BackgroundRunner.