Typings — Async
Import
import type {
IPromiseResolver, IPromiseRejecter,
IWithTimeoutOptions, IWithAbortSignalOptions,
IRetryOptions, IRetryContext, IBeforeRetryCallback,
IRetryDelayGenerator, IRetryDelayJitterFunction, IRetryDelayOptions,
IFiberContext, IFiberExecution, IFiberOptions,
IBackgroundRunnerOptions, IBackgroundRunnerEvents,
} from '@litert/utils-async';IPromiseResolver<T>
Source: Typings.ts
Function signature type for a Promise's resolve callback.
type IPromiseResolver<T = unknown> = (value?: T | PromiseLike<T>) => void;IPromiseRejecter<T>
Source: Typings.ts
Function signature type for a Promise's reject callback.
type IPromiseRejecter<T = unknown> = (reason: T) => void;IWithTimeoutOptions<T>
Source: WithTimeout.ts
Options for withTimeout.
interface IWithTimeoutOptions<T> {
collectResult?: (error: unknown | null, result?: T) => void;
}Properties
collectResult?: (error: unknown | null, result?: T) => voidCalled with the eventual outcome of the underlying task after the timeout fires. Use this if you care about the late-arriving result or error. If the task eventually succeeds,
errorisnullandresultholds the value. If it fails,errorholds the reason.
IWithAbortSignalOptions<T>
Source: WithAbortSignal.ts
Options for withAbortSignal.
interface IWithAbortSignalOptions<T> {
collectResult?: (error: unknown | null, result?: T) => void;
}Properties
Same semantics as IWithTimeoutOptions.collectResult — called with the task's eventual result after the abort signal fires.
IRetryOptions<TResult>
Source: AutoRetry.ts
Options for autoRetry.
interface IRetryOptions<TResult> {
maxRetries: number;
function: (context: IRetryContext) => Promise<TResult>;
beforeRetry?: IBeforeRetryCallback;
signal?: AbortSignal;
}Properties
maxRetries: number— Maximum number of retries after the first call fails. Must be a positive integer.function— The async function to try. Receives anIRetryContextobject.beforeRetry?: IBeforeRetryCallback(default:DEFAULT_BEFORE_RETRY) — Called before each retry. Throw to stop retrying. Use it for delay, logging, or condition checks.signal?: AbortSignal— Optional abort signal to cancel the retry loop.
IRetryContext
Context passed to the function and beforeRetry callbacks of autoRetry.
interface IRetryContext {
readonly retriedTimes: number;
readonly error: unknown;
readonly signal?: AbortSignal;
}Properties
retriedTimes: number— How many retries have been performed so far (0on the first call).error: unknown— The last error thrown by the main function (nullon the first call).signal?: AbortSignal— The abort signal passed inIRetryOptions.signal.
IBeforeRetryCallback
type IBeforeRetryCallback = (context: IRetryContext) => void | Promise<void>;The type of the callback called before each retry attempt. Throwing an error stops the retry loop.
IRetryDelayGenerator
interface IRetryDelayGenerator {
(attempt: number): number;
}A function that returns the delay in milliseconds for a given retry attempt number (0-indexed).
IRetryDelayJitterFunction
interface IRetryDelayJitterFunction {
(delay: number): number;
}A function that applies randomization (jitter) to a raw delay value.
IRetryDelayOptions
Options for compositeRetryDelayGenerator.
interface IRetryDelayOptions {
delayGenerator: IRetryDelayGenerator;
jitter: IRetryDelayJitterFunction;
maxDelay: number;
}Properties
delayGenerator— Base delay generator (e.g., exponential backoff).jitter— Jitter function to apply on top of the raw delay.maxDelay— Upper bound on the final delay in milliseconds.
IFiberContext<T>
Source: FiberController.ts
The context object passed to the fiber execution function.
interface IFiberContext<T = IDict> {
readonly data: T;
readonly signal: AbortSignal;
sleep(): Promise<void>;
}Properties
data: T— Shared data between the fiber and the controller.signal: AbortSignal— Abort signal for the fiber. Pass to other async operations to support cancellation.sleep(): Promise<void>— Suspends the fiber untilFiberController.resumeis called. Throws if the fiber has been aborted.
IFiberExecution<T>
type IFiberExecution<T = IDict> = (ctx: IFiberContext<T>) => Promise<void>;The signature of the function passed as main when creating a FiberController.
IFiberOptions<T>
Constructor options for FiberController<T>.
// When T is null:
type IFiberOptions<null> = { main: IFiberExecution<null>; data?: null };
// When T is not null:
type IFiberOptions<T> = { main: IFiberExecution<T>; data: T };Properties
main: IFiberExecution<T>— The async function that runs as the fiber body.data: T— Shared data object. Required whenTis notnull.
IBackgroundRunnerOptions
Source: BackgroundRunner.ts
Constructor options for BackgroundRunner.
interface IBackgroundRunnerOptions {
waitFn?: () => Promise<void>;
}Properties
waitFn?: () => Promise<void>(default:DEFAULT_BG_RUNNER_WAIT_FN) — The wait function called byrunLaterbefore executing callbacks.
IBackgroundRunnerEvents
Events emitted by BackgroundRunner.
interface IBackgroundRunnerEvents {
'error': [error: unknown];
}Events
'error'— Emitted when a background callback throws an unhandled error or rejects.
Constants
DEFAULT_BG_RUNNER_WAIT_FN
Source: packages/partials/async/src/Classes/BackgroundRunner.ts
The default wait function used by BackgroundRunner.runLater. It calls sleep(0), which yields control to the event loop immediately.
import { DEFAULT_BG_RUNNER_WAIT_FN } from '@litert/utils-async';
const DEFAULT_BG_RUNNER_WAIT_FN: () => Promise<void>;DEFAULT_BEFORE_RETRY
The default beforeRetry callback used by autoRetry when no custom beforeRetry is provided.
Applies exponential backoff with full jitter and a maximum delay of 30 seconds:
- Base delay: 1000 ms
- Factor: 2 (doubles each attempt)
- Jitter: full (random within
[0, delay)) - Max delay: 30000 ms
import { DEFAULT_BEFORE_RETRY } from '@litert/utils-async';
const DEFAULT_BEFORE_RETRY: IBeforeRetryCallback;