node:timers
The node:timers module in Azion Runtime: timeouts, intervals, immediates, the promise-based timers, examples, and the supported APIs.
The node:timers module holds the Node.js functions that schedule code to run after a delay or at regular intervals. Use it to manage asynchronous work and to control when each part of a function runs: defer work, add a short delay, or coordinate asynchronous tasks with setTimeout(). Azion Runtime provides the module through its Node.js compatibility, and the promise-based timers come from node:timers/promises.
Examples
Each example is a complete function. Where an example shows a response, it is the one a deployed function returns.
Basic setTimeout
This function logs two lines, waits 2,000 ms on a timer wrapped in a promise, and then responds:
The function responds after the wait:
Delayed execution with setTimeout
This function schedules two sequential 100 ms delays and returns the steps it logged and the total time in milliseconds:
The setTimeoutAsync helper wraps timers.setTimeout() in a promise. In Azion Runtime, util.promisify() does not turn timers.setTimeout() into a promise-based timer: the promisified function passes the delay where the callback goes, and the call throws EvalError: eval not allowed on setTimeout/setInterval parameter. Deployed, totalTimeMs does not measure the wait, because Date.now() does not advance during a request.
Periodic operations with setInterval
This function runs a callback on a 50 ms interval for 250 ms, clears the interval, and returns every tick with its elapsed time:
The function responds with four ticks:
Every elapsed value and durationMs are 0 because, deployed, Date.now() does not advance during a request. performance.now() does advance. For more information, refer to Globals.
Immediate execution with setImmediate
This function queues a setImmediate() callback, a setTimeout() callback with a 0 ms delay, and a process.nextTick() callback, then records the order in which they run:
The function responds with the order of the callbacks: setImmediate() first, then setTimeout() with a 0 ms delay, then process.nextTick():
Timeout with cancellation
This function cancels a 500 ms timeout before it fires, then calls an API with a timeout that aborts the request through an AbortController:
The function responds with the cancellation and the API result. The cancelled callback never adds Timeout executed:
Timer promises
This function uses the promise-based timers of node:timers/promises, which the Node.js timers/promises documentation describes:
The function responds with the steps it logged:
The async iterator that setInterval() returns yields one value and then ends, so the loop logs Interval tick 1 only. The elapsed values read 0 because, deployed, Date.now() does not advance during a request.
Retry logic with exponential backoff
This function retries a failing operation with an exponential backoff from a 50 ms initial delay, races a 200 ms operation against a 50 ms timeout, and returns both results as JSON:
The setTimeoutAsync helper wraps timers.setTimeout() in a promise, the form that waits for its delay in Azion Runtime. A helper built with util.promisify(timers.setTimeout) throws EvalError: eval not allowed on setTimeout/setInterval parameter on its first delay.
Supported APIs
The table lists the status of each node:timers and node:timers/promises API in Azion Runtime:
| API | Status |
|---|---|
setTimeout() | 🟢 Supported |
clearTimeout() | 🟢 Supported |
setInterval() | 🟢 Supported |
clearInterval() | 🟢 Supported |
setImmediate() | 🟢 Supported |
clearImmediate() | 🟢 Supported |
timers/promises.setTimeout() | 🟡 Partially supported |
timers/promises.setInterval() | 🟡 Partially supported |
timers/promises.setImmediate() | 🟢 Supported |
timers.active() | 🟡 Partially supported |
timers.enroll() | 🔴 Not supported |
timers.unenroll() | 🔴 Not supported |
timers.getActive() | 🟡 Partially supported |
APIs marked 🟡 Partially supported have limited functionality compared to the full Node.js implementation. timers/promises.setTimeout() resolves with the value passed to it; under azion dev, it resolves at once and does not wait for the delay. The async iterator that timers/promises.setInterval() returns yields one value and then ends. To wait for a delay, wrap timers.setTimeout() in a promise: new Promise((resolve) => timers.setTimeout(resolve, ms)). Node.js deprecates enroll() and unenroll(), and Azion Runtime does not support them.
A timer callback must be a function. Deployed, a string or a number in its place throws EvalError: eval not allowed on setTimeout/setInterval parameter. timers.setTimeout() returns a timer object with ref() and unref() methods, and its refresh() is undefined. The node:timers/promises module exports scheduler, setImmediate, setInterval, and setTimeout, and a default export.