Note
To enable built-in Node.js APIs and polyfills, add the nodejs_compat compatibility flag to your Wrangler configuration file. This also enables nodejs_compat_v2 as long as your compatibility date is 2024-09-23 or later. Learn more about the Node.js compatibility flag and v2.
Use node:timers
â APIs to schedule functions to be executed later.
This includes setTimeout
â for calling a function after a delay, setInterval
â for calling a function repeatedly, and setImmediate
â for calling a function in the next iteration of the event loop.
import timers from "node:timers";
export default {
async fetch() {
console.log("first");
const { promise1, resolve1 } = Promise.withResolvers();
const { promise2, resolve2 } = Promise.withResolvers();
timers.setTimeout(() => {
console.log("last");
resolve1();
}, 10);
timers.setTimeout(() => {
console.log("next");
resolve2();
});
await Promise.all([promise1, promise2]);
return new Response("ok");
},
};
import timers from "node:timers";
export default {
async fetch() {
console.log("first");
const { promise1, resolve1 } = Promise.withResolvers();
const { promise2, resolve2 } = Promise.withResolvers();
timers.setTimeout(() => {
console.log("last");
resolve1();
}, 10);
timers.setTimeout(() => {
console.log("next");
resolve2();
});
await Promise.all([promise1, promise2]);
return new Response("ok");
}
}
Note
Due to security-based restrictions on timers in Workers, timers are limited to returning the time of the last I/O. This means that while setTimeout, setInterval, and setImmediate will defer your function execution until after other events have run, they will not delay them for the full time specified.
Note
When called from a global level (on globalThis
â), functions such as clearTimeout
and setTimeout
will respect web standards rather than Node.js-specific functionality. For complete Node.js compatibility, you must call functions from the node:timers
module.
The full node:timers
API is documented in the Node.js documentation for node:timers
â.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4