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.

Hierarchy (view full)

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

logPrefix: string = ...

A string prefix for all log statements made by this target.

timeout: number

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

Accessors

  • get [toStringTag](): string
  • Returns string

  • get aborted(): boolean
  • True if this signal is aborted.

    Returns boolean

  • get nativeSignal(): null | AbortSignal
  • Returns a native signal on supported platforms.

    Returns null | AbortSignal

  • get reason(): null | Error
  • A JavaScript value providing the abort reason, once this signal has aborted.

    Returns null | Error

Methods

  • Sets to the aborted state with the given reason, and emits an 'abort' event. If this signal is already aborted, this will do nothing.

    Parameters

    • reason: Error = ...

    Returns void

  • Aborts with a TimeoutError

    Returns void

  • Dispatches an event.

    Type Parameters

    • K extends "abort"

    Parameters

    • type: K

      The key of the event within EventMap

    • event: AbortEventMap[K]

      If the passed event is read-only, then it may be re-used/cached. Otherwise, it should be a new event object every dispatch. If no target property is set on the event, the target will be set to this host.

    Returns void

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

  • 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 this signal's abort reason if the signal has been aborted; otherwise it does nothing.

    Returns void