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.

The example below shows how to use the timers module in a function:

/**
* An example of using Node.js Timers API in an Azion Function.
* Support:
* - Partially supported (Extended by library `timers-browserify`)
* @module runtime-apis/nodejs/timers/main
* @example
* // Execute with Azion Bundler:
* npx edge-functions build
*
*/
import timers from "node:timers";
/**
* An example of using the Node.js Timers API in an Azion Function.
* @param {*} event
* @returns {Promise<Response>}
*/
const main = async (event) => {
console.log("Hello, world!");
console.log("Waiting for 5 seconds...");
await new Promise((resolve) => timers.setTimeout(resolve, 2000));
return new Response("Done!", { status: 200 });
};
export default main;