Class Timer<T>

Just like setTimeout or setInterval, this will keep the NodeJS event loop alive

Type Parameters

  • T = void

Hierarchy

  • Timer

Implements

  • Pick<PromiseCancellable<T>, keyof PromiseCancellable<T>>

Constructors

  • Construct a Timer By default lazy is false, which means it will eagerly reject the timer, even if the handler has already started executing If lazy is true, this will make the timer wait for the handler to finish executing Note that passing a custom controller does not stop the default behaviour

    Type Parameters

    • T = void

    Parameters

    • Optional handler: ((signal) => T | PromiseLike<T>)
        • (signal): T | PromiseLike<T>
        • Parameters

          • signal: AbortSignal

          Returns T | PromiseLike<T>

    • Optional delay: number
    • Optional lazy: boolean
    • Optional controller: PromiseCancellableController

    Returns Timer<T>

  • Type Parameters

    • T = void

    Parameters

    • Optional opts: {
          controller?: PromiseCancellableController;
          delay?: number;
          handler?: ((signal) => T | PromiseLike<T>);
          lazy?: boolean;
      }
      • Optional controller?: PromiseCancellableController
      • Optional delay?: number
      • Optional handler?: ((signal) => T | PromiseLike<T>)
          • (signal): T | PromiseLike<T>
          • Parameters

            • signal: AbortSignal

            Returns T | PromiseLike<T>

      • Optional lazy?: boolean

    Returns Timer<T>

Properties

_delay: number

Delay in milliseconds This may be Infinity

_scheduled?: Date

Timestamp when this is scheduled to finish and execute the handler Guaranteed to be weakly monotonic within the process lifetime Compare this with performance.now() not Date.now()

_status: null | "settling" | "settled" = null

The status indicates when we have started settling or settled

abortController: AbortController

Abort controller allows immediate cancellation

handler?: ((signal) => T | PromiseLike<T>)

Type declaration

    • (signal): T | PromiseLike<T>
    • Handler to be executed

      Parameters

      • signal: AbortSignal

      Returns T | PromiseLike<T>

lazy: boolean

If it is lazy, the timer will not eagerly reject on cancellation if the handler has started executing

p: PromiseCancellable<T>

Deconstructed promise

rejectP: ((reason?) => void)

Type declaration

    • (reason?): void
    • Reject deconstructed promise

      Parameters

      • Optional reason: any

      Returns void

resolveP: ((value?) => void)

Type declaration

    • (value?): void
    • Resolve deconstructed promise

      Parameters

      • Optional value: T

      Returns void

timeoutRef?: Timeout

Internal timeout reference

timestamp: Date

Timestamp when this is constructed Guaranteed to be weakly monotonic within the process lifetime Compare this with performance.now() not Date.now()

Accessors

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

  • get delay(): number
  • Delay in milliseconds This may be Infinity

    Returns number

  • get scheduled(): undefined | Date
  • Timestamp when this is scheduled to finish and execute the handler Guaranteed to be weakly monotonic within the process lifetime Compare this with performance.now() not Date.now()

    Returns undefined | Date

  • get status(): null | "settling" | "settled"
  • Returns null | "settling" | "settled"

Methods

  • Cancels the timer Unlike PromiseCancellable, canceling the timer will not result in an unhandled promise rejection, all promise rejections are ignored

    Parameters

    • Optional reason: any

    Returns void

  • Type Parameters

    • TResult = never

    Parameters

    • Optional onRejected: null | ((reason, signal) => TResult | PromiseLike<TResult>)
    • Optional controller: PromiseCancellableController

    Returns PromiseCancellable<T | TResult>

  • Parameters

    • Optional onFinally: null | ((signal) => void)
    • Optional controller: PromiseCancellableController

    Returns PromiseCancellable<T>

  • Gets the remaining time in milliseconds This will return Infinity if delay is Infinity This will return 0 if status is settling or settled

    Returns number

  • Refreshes the timer to the original delay and updates the scheduled time.

    Returns void

    Throws

    If the timer is settling or settled

  • Parameters

    • Optional reason: any

    Returns Promise<void>

  • Resets the timer with a new delay and updates the scheduled time and delay.

    Parameters

    • delay: number

    Returns void

    Throws

    If the timer is settling or settled

  • Type Parameters

    • TResult1 = T

    • TResult2 = never

    Parameters

    • Optional onFulfilled: null | ((value, signal) => TResult1 | PromiseLike<TResult1>)
    • Optional onRejected: null | ((reason, signal) => TResult2 | PromiseLike<TResult2>)
    • Optional controller: PromiseCancellableController

    Returns PromiseCancellable<TResult1 | TResult2>

  • To remaining time as a string This may return 'Infinity' if this.delay is Infinity This will return '0' if status is settling or settled

    Returns string

  • To remaining time as a number This may return Infinity if this.delay is Infinity This will return 0 if status is settling or settled

    Returns number

Generated using TypeDoc