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 { promise: promise1, resolve: resolve1 } = Promise.withResolvers();
const { promise: promise2, resolve: 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(): Promise<Response> {
console.log("first");
const { promise: promise1, resolve: resolve1 } = Promise.withResolvers<void>();
const { promise: promise2, resolve: resolve2 } = Promise.withResolvers<void>();
timers.setTimeout(() => {
console.log("last");
resolve1();
}, 10);
timers.setTimeout(() => {
console.log("next");
resolve2();
});
await Promise.all([promise1, promise2]);
return new Response("ok");
}
} satisfies ExportedHandler<Env>;The full node:timers API is documented in the Node.js documentation for node:timers ↗.