Skip to main content

Module timers

Module timers 

Source
Expand description

setTimeout / setInterval / clearTimeout / clearInterval / setImmediate / queueMicrotask — native, ctx.spawn-backed (the timer future lives on the host’s VM executor, so callbacks fire between executes and while a script is parked on a host await; dropping the runtime aborts every armed timer).

The timer handle is a Timeout class instance (not a numeric id): it survives REPL-style across evaluations via globalThis and clearTimeout(handle) cancels through its Notify. Holding the JS callback inside the spawned future is the sanctioned executor-owned-future shape (same as AbortSignal.timeout) — the future is dropped with the runtime, never stored in a traced JS field.

The delay follows the HTML spec rather than Node. A setTimeout(fn, 0) runs on the next turn of the event loop, after the microtask checkpoint, instead of waiting out Node’s unconditional one millisecond – for a script that polls with await sleep(0) that is the difference between microseconds and milliseconds per turn. What keeps that from starving the loop is the spec’s own guard, the timer NESTING LEVEL: a timeout armed from inside a timer callback is one level deeper than the callback’s own, and past level five a delay under 4ms is raised to 4ms. setInterval deepens a level per repeat, so a zero-delay interval free-runs a few times and then settles at 4ms, which is what a browser does.

A host with ambient per-callback state (a capability grant, a request scope) supplies it as a CallbackPolicy: it is captured when the timer is armed and re-entered when the callback fires, so a callback registered under a restriction keeps it instead of falling back to whatever the resting state happens to be. Hosts without such state install NoPolicy.

Structs§

NoPolicy
For hosts with no ambient callback state: callbacks run as they are.
Timeout
Opaque timer handle returned by setTimeout / setInterval.

Traits§

CallbackPolicy
Ambient host state that a scheduled callback must run under.

Functions§

install
Install the timer globals, carrying P from registration to callback.