Function deadline

  • Create a promise which will be rejected with DOMException when a given delay is exceeded.

    Note: Prefer to use AbortSignal.timeout instead for the APIs that accept AbortSignal.

    Type Parameters

    • T

      The type of the provided and returned promise.

    Parameters

    • p: Promise<T>

      The promise to make rejectable.

    • ms: number

      Duration in milliseconds for when the promise should time out.

    • options: DeadlineOptions = {}

      Additional options.

    Returns Promise<T>

    A promise that will reject if the provided duration runs out before resolving.

    If the provided duration runs out before resolving.

    If the optional signal is aborted with the default reason before resolving or timing out.

    If the optional signal is aborted with a custom reason before resolving or timing out.

    import { deadline } from "@std/async/deadline";
    import { delay } from "@std/async/delay";

    const delayedPromise = delay(1_000);
    // Below throws `DOMException` after 10 ms
    const result = await deadline(delayedPromise, 10);