Dev Helpers

Node.js / Timers and cancellation

Resolve after a delay

Dev Helpers · April 7, 2026

A promise-based delay returns control while a timer waits. It establishes a minimum delay, not an exact execution timestamp.

Worked example

import {
  setTimeout as delay,
  setImmediate as immediate,
} from "node:timers/promises";
console.log(await delay(1, "done"));

Result

done

Implementation notes

Timer work competes with other callbacks. For recurring sampling, compare actual timestamps rather than assuming every tick arrived exactly on schedule.

Try it yourself

Schedule the same delay while another operation occupies the event loop.

Open the Node.js example →

Editorial date April 7, 2026 · Published December 6, 2026

Related articles

Safely parse malformed input

Parsing user-provided JSON needs a failure path. Successful parsing alone does not establish that the result has the expected shape.

Checksum bytes with CRC32

CRC32 is useful for detecting accidental changes to a byte sequence. Record the input encoding so different producers compute the same checksum.

Attach details to a mark

Performance marks can carry details about the operation they describe. Small stable identifiers are easier to aggregate than large payloads.