Skip to content

Typings — Async

Import

ts
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.

ts
type IPromiseResolver<T = unknown> = (value?: T | PromiseLike<T>) => void;

IPromiseRejecter<T>

Source: Typings.ts

Function signature type for a Promise's reject callback.

ts
type IPromiseRejecter<T = unknown> = (reason: T) => void;

IWithTimeoutOptions<T>

Source: WithTimeout.ts

Options for withTimeout.

ts
interface IWithTimeoutOptions<T> {
    collectResult?: (error: unknown | null, result?: T) => void;
}

Properties

  • collectResult?: (error: unknown | null, result?: T) => void

    Called 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, error is null and result holds the value. If it fails, error holds the reason.


IWithAbortSignalOptions<T>

Source: WithAbortSignal.ts

Options for withAbortSignal.

ts
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.

ts
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 an IRetryContext object.
  • 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.

ts
interface IRetryContext {
    readonly retriedTimes: number;
    readonly error: unknown;
    readonly signal?: AbortSignal;
}

Properties

  • retriedTimes: number — How many retries have been performed so far (0 on the first call).
  • error: unknown — The last error thrown by the main function (null on the first call).
  • signal?: AbortSignal — The abort signal passed in IRetryOptions.signal.

IBeforeRetryCallback

ts
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

ts
interface IRetryDelayGenerator {
    (attempt: number): number;
}

A function that returns the delay in milliseconds for a given retry attempt number (0-indexed).


IRetryDelayJitterFunction

ts
interface IRetryDelayJitterFunction {
    (delay: number): number;
}

A function that applies randomization (jitter) to a raw delay value.


IRetryDelayOptions

Options for compositeRetryDelayGenerator.

ts
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.

ts
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 until FiberController.resume is called. Throws if the fiber has been aborted.

IFiberExecution<T>

ts
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>.

ts
// 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 when T is not null.

IBackgroundRunnerOptions

Source: BackgroundRunner.ts

Constructor options for BackgroundRunner.

ts
interface IBackgroundRunnerOptions {
    waitFn?: () => Promise<void>;
}

Properties

  • waitFn?: () => Promise<void> (default: DEFAULT_BG_RUNNER_WAIT_FN) — The wait function called by runLater before executing callbacks.

IBackgroundRunnerEvents

Events emitted by BackgroundRunner.

ts
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.

ts
import { DEFAULT_BG_RUNNER_WAIT_FN } from '@litert/utils-async';

const DEFAULT_BG_RUNNER_WAIT_FN: () => Promise<void>;

DEFAULT_BEFORE_RETRY

Source: packages/partials/async/src/Functions/AutoRetry.ts

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
ts
import { DEFAULT_BEFORE_RETRY } from '@litert/utils-async';

const DEFAULT_BEFORE_RETRY: IBeforeRetryCallback;