A TimeoutController is an ReadonlyAbort implementation that will abort either after an elapsed timeout, an explicit abort call, or if the optional external abort signal fires.

This allows a function (such as the network fetch layer) to abort from an externally provided abort signal, while maintaining its own timeout and abort logic internally.

Implements

Constructors

  • Parameters

    • timeout: number

      The number of seconds before the returned abort signal will abort with a TimeoutError.

    • Optionalabort: Maybe<ReadonlyAbort>

      An abort signal to cascade.

    • message: string = 'Timed out after {0}s'

      The timeout message. Uses one token {0} which will be replaced by the timeout.

    Returns TimeoutController

Properties

timeout: number

The number of seconds before the returned abort signal will abort with a TimeoutError.

Accessors

  • get aborted(): boolean
  • True if aborted.

    Returns boolean

  • get reason(): null | Error
  • The reason passed to abort.

    Returns null | Error

  • get signal(): null | AbortSignal
  • The native signal, if supported.

    Returns null | AbortSignal

Methods

  • Aborts with the given reason.

    Parameters

    • reason: Error = ...

    Returns void

  • Aborts with a TimeoutError

    Returns void

  • Returns void

  • Returns true if the event host has any listeners.

    Returns boolean

  • Returns true if the event host has any listeners for the given type.

    Parameters

    • type: "abort"

    Returns boolean

  • Adds an event handler for the given typed event.

    Type Parameters

    • K extends "abort"

    Parameters

    • type: K

      The key representing the event type to listen for.

    • handler: EventHandler<AbortEventMap[K]>

      A callback to invoke when the event with the given type is dispatched.

    • Optionaloptions: SignalOptions

      Options for changing listening behavior.

    Returns Unsubscribe

    Returns a method, when invoked, removes the handler.

  • Invokes a callback on 'abort', or immediately if already aborted.

    Parameters

    • callback: (() => void)
        • (): void
        • Returns void

    Returns Unsubscribe

  • Sleeps the given amount of time, or immediately rejects if the sleep will cause a timeout.

    Parameters

    • time: number

      The number of seconds to sleep.

    Returns Promise<void>

  • Throws the signal's abort reason if the signal has been aborted; otherwise it does nothing.

    Returns void