LogoPear Docs
ReferencesBareModules

bare-timers

Native timers for Javascript

Documented against v3.2.1
stable

bare-timers — Native timers for Javascript. It is a native addon and requires Bare >=1.7.0.

Mirrors the Node.js timers module.

npm i bare-timers

Usage

const { setTimeout, clearTimeout } = require('bare-timers')

API

Functions

setTimeout

setTimeout<T extends unknown[]>(callback: (...args: T) => unknown, delay: number, ...args: T): Timeout

Schedule execution once after delay milliseconds, clamped to a minimum of 1ms.

Parameters

ParameterTypeDefaultDescription
callback(...args: T) => unknownThe function to run after the delay.
delaynumberMilliseconds to wait before running; clamped to a minimum of 1.
argsTAdditional arguments passed to callback.

clearTimeout(timer: Timeout): void

Cancel a pending timeout, preventing it from firing.

Parameters

ParameterTypeDefaultDescription
timerTimeoutThe timeout handle to cancel.

setInterval

setInterval<T extends unknown[]>(callback: (...args: T) => unknown, delay: number, ...args: T): Timeout

Schedule repeated execution every delay milliseconds, clamped to a minimum of 1ms.

Parameters

ParameterTypeDefaultDescription
callback(...args: T) => unknownThe function to run on each interval.
delaynumberMilliseconds between runs; clamped to a minimum of 1.
argsTAdditional arguments passed to callback.

clearInterval(timer: Timeout): void

Cancel a pending interval, preventing further firings.

Parameters

ParameterTypeDefaultDescription
timerTimeoutThe interval handle to cancel.

setImmediate<T extends unknown[]>(callback: (...args: T) => unknown, ...args: T): Immediate

Schedule execution once at the end of the current event loop iteration.

Parameters

ParameterTypeDefaultDescription
callback(...args: T) => unknownThe function to run at the end of the current event loop iteration.
argsTAdditional arguments passed to callback.

clearImmediate(immediate: Immediate): void

Cancel a pending immediate, preventing it from firing.

Parameters

ParameterTypeDefaultDescription
immediateImmediateThe immediate handle to cancel.

Types

Task

interface Task {
  ref(): this
  unref(): this
  hasRef(): boolean
}

The base handle shared by Timeout and Immediate, controlling whether it keeps the event loop alive.

Timeout

interface Timeout {
  refresh(): this
  ref(): this
  unref(): this
  hasRef(): boolean
}

The handle returned by setTimeout and setInterval.

Immediate

interface Immediate {
  ref(): this
  unref(): this
  hasRef(): boolean
}

The handle returned by setImmediate.

bare-timers/promises

Functions

setTimeout<T>(delay?: number, value?: T, options?: TimeoutOptions): Promise<T>

Schedule execution once after delay milliseconds, clamped to a minimum of 1ms.

Parameters

ParameterTypeDefaultDescription
delay?numberMilliseconds to wait before running; clamped to a minimum of 1.
value?TThe value the returned promise resolves with.
options?TimeoutOptionsOptions; ref defaults to true (set false to unref), and signal may be an AbortSignal that cancels the timer.

setInterval<T>(delay?: number, value?: T, options?: TimeoutOptions): AsyncGenerator<T>

Schedule repeated execution every delay milliseconds, clamped to a minimum of 1ms.

Parameters

ParameterTypeDefaultDescription
delay?numberMilliseconds between runs; clamped to a minimum of 1.
value?TThe value yielded on each iteration.
options?TimeoutOptionsOptions; ref defaults to true (set false to unref), and signal may be an AbortSignal that cancels the timer.

setImmediate<T>(value?: T, options?: ImmediateOptions): Promsie<T>

Schedule execution once at the end of the current event loop iteration.

Parameters

ParameterTypeDefaultDescription
value?TThe value the returned promise resolves with.
options?ImmediateOptionsOptions; ref defaults to true (set false to unref), and signal may be an AbortSignal that cancels the timer.

Types

TaskOptions

interface TaskOptions {
  ref?: boolean
  signal?: AbortSignal
}

Shared options for the bare-timers/promises scheduling functions.

TimeoutOptions

interface TimeoutOptions {
  ref?: boolean
  signal?: AbortSignal
}

Options for bare-timers/promises' setTimeout and setInterval.

ImmediateOptions

interface ImmediateOptions {
  ref?: boolean
  signal?: AbortSignal
}

Options for bare-timers/promises' setImmediate.

See also

  • Inside Bare these functions are installed as globals, so most code calls them directly without importing anything.
  • delay is floored to an integer. A delay that is less than 1, NaN, non-numeric, or greater than Number.MAX_SAFE_INTEGER is clamped to 1 ms. (Node caps the maximum at 2147483647; Bare's ceiling is Number.MAX_SAFE_INTEGER.)
  • Passing null, undefined, or a non-object to clearTimeout/clearInterval/clearImmediate is a no-op, as is clearing a handle that has already fired or been cleared. All three delegate to the same routine, so any one can cancel any handle—but use the matching name for clarity.
  • Every handle also implements [Symbol.dispose](), so a using timer = setTimeout(…) declaration cancels the timer when the scope exits.
  • The usual suspension pattern is to clear timers outright on suspend; unref() is the alternative for a timer that must keep running across the cycle—the same pattern bare-ipc uses for its channel.
  • Hooks into the Bare runtime's lifecycle events (idle, resume, wakeup) to pause and restart the underlying native timer. The promise-based API integrates with bare-abort-controller—an optional peer dependency—for signal-based cancellation.
  • require('bare-timers').promises is also reachable as the standalone require('bare-timers/promises').
  • Bare modules — the full bare-* catalog.
  • Bare runtime API — the runtime these modules extend.

On this page