Node.js API compatibility - Timers
Explore the integration and function of the Node.js Timers module within the Azion Runtime. The Timers module provides the ability to schedule code execution after a set period or at regular intervals.
The timers module in Node.js provides a set of functions for scheduling the execution of code after a specified delay or at regular intervals. It’s essential for managing asynchronous operations and controlling the timing of function execution within applications. This module is available in the Azion Runtime through Node.js compatibility, and it’s commonly used in functions to defer work, add small delays, or coordinate the timing of asynchronous tasks with setTimeout.
Example: Basic setTimeout
The example below shows how to use the timers module in a function:
Example: Delayed execution with setTimeout
Schedule operations to run after a delay:
Example: setInterval for periodic operations
Execute code at regular intervals:
Example: Immediate execution with setImmediate
Execute code immediately after the current event loop:
Example: Timeout with cancellation
Implement cancellable timeouts for API calls:
Example: Timer promises (timers/promises)
Use the modern promise-based timer API:
Example: Retry logic with exponential backoff
Implement retry patterns using timers:
Supported APIs
| API | Status |
|---|---|
setTimeout() | 🟢 Supported |
clearTimeout() | 🟢 Supported |
setInterval() | 🟢 Supported |
clearInterval() | 🟢 Supported |
setImmediate() | 🟢 Supported |
clearImmediate() | 🟢 Supported |
timers/promises.setTimeout() | 🟢 Supported |
timers/promises.setInterval() | 🟢 Supported |
timers/promises.setImmediate() | 🟢 Supported |
timers.active() | 🟡 Partially supported |
timers.enroll() | 🔴 Not supported |
timers.unenroll() | 🔴 Not supported |
timers.getActive() | 🟡 Partially supported |