Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Cloudflare Docs
Log in

Usenode:timers APIs to schedule functions to be executed later.

This includessetTimeout for calling a function after a delay,setInterval for calling a function repeatedly,andsetImmediate for calling a function in the next iteration of the event loop.

index.js
importtimers from"node:timers";
exportdefault{
asyncfetch(){
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();
});
awaitPromise.all([promise1,promise2]);
returnnewResponse("ok");
},
};

Due tosecurity-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 executionuntil after other events have run, they will not delay them for the full time specified.

When called from a global level (onglobalThis),functions such asclearTimeout andsetTimeout will respect web standards rather than Node.js-specific functionality. For complete Node.jscompatibility, you must call functions from thenode:timers module.

The fullnode:timers API is documented in theNode.js documentation fornode:timers.


[8]
ページ先頭

©2009-2026 Movatter.jp