Skip to main content

Timers & Scheduling

Timers express eligibility, not real-time guarantees. setTimeout(fn, 100) means “do not run before roughly this threshold,” then the callback still waits for event-loop availability.

Scheduling families​

APIQueue/intentTypical use
process.nextTick()Node-specific next-tick queuecompatibility/small deferred work; avoid recursion
queueMicrotask()JS microtaskpost-stack microtask semantics
Promise reactionJS microtaskasync continuation
setImmediate()check phaseyield until later loop turn / after I/O
setTimeout()timersdelay/deadline scheduling

Do not build correctness on accidental ordering between timers and immediates from unrelated contexts.

Drift​

setInterval(async () => {
await expensiveJob();
}, 1000);

If the job lasts longer than the interval, work may overlap or drift depending on design. For periodic jobs, decide explicitly whether you need fixed-rate, fixed-delay, no-overlap, distributed ownership, persistence, and missed-run semantics.

A safer in-process recursive delay:

async function loop() {
try { await runOnce(); }
finally { setTimeout(loop, 1_000).unref(); }
}
loop();

ref() / unref()​

A referenced timer can keep the process alive. unref() allows the process to exit if nothing else remains. This changes lifecycle, not callback priority.

Production rule​

In-process timers are good for local ephemeral maintenance. They are poor substitutes for durable schedulers when a task must survive crashes, deployments, horizontal scaling, or leader changes.