Skip to content

Errors — Async

Source: Errors.ts

Import

ts
import { TimeoutError, AbortedError } from '@litert/utils-async';

TimeoutError

Thrown when an asynchronous operation exceeds its configured timeout. Extends Error.

ts
class TimeoutError extends Error {
    readonly name: 'TimeoutError';
    readonly message: 'Operation timed out';
    readonly unresolvedPromise: Promise<unknown>;

    constructor(unresolvedPromise: Promise<unknown>);
}

Properties

  • unresolvedPromise: Promise<unknown>

    A reference to the underlying Promise that was still pending when the timeout fired. You can use this if you need to collect or discard the eventual result. See IWithTimeoutOptions.collectResult for a structured alternative.

Usage

ts
import { withTimeout, TimeoutError } from '@litert/utils-async';

try {
    await withTimeout(1000, fetch('https://example.com/slow'));
} catch (e) {
    if (e instanceof TimeoutError) {
        console.error('Timed out after 1 second');
    }
}

AbortedError

Thrown when an asynchronous operation is cancelled via an AbortSignal. Extends DOMException (name: 'AbortError').

ts
class AbortedError extends DOMException {
    readonly name: 'AbortError';
    readonly message: 'Operation aborted.';
    readonly unresolvedPromise: Promise<unknown> | null;

    constructor(unresolvedPromise: Promise<unknown> | null);
}

Properties

  • unresolvedPromise: Promise<unknown> | null

    A reference to the still-pending Promise, or null if the signal was already aborted before the task started (or if no task was launched).

Usage

ts
import { sleep, AbortedError } from '@litert/utils-async';

const ac = new AbortController();
setTimeout(() => ac.abort(), 500);

try {
    await sleep(2000, ac.signal);
} catch (e) {
    if (e instanceof AbortedError) {
        console.log('Sleep was aborted');
    }
}