Sleep and delay execution
Scripts often need to wait: between retries, while polling, or to pace requests. This example shows how to sleep without blocking, and how to make a sleep cancelable.
The simplest sleep is a promise around setTimeout.
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
console.log("waiting...");
await sleep(100);
console.log("done"); // prints 100ms laterThe same helper ships with the Node.js compatibility APIs as a promised setTimeout, with an optional resolution value.
import { setTimeout as nodeSleep } from "node:timers/promises";
console.log(await nodeSleep(100, "woke up")); // woke upThe standard library provides the same thing as delay, with extras.
import { delay } from "jsr:@std/async/delay";
await delay(100);A delay accepts an AbortSignal, which makes it cancelable. Aborting rejects the promise.
const controller = new AbortController();
setTimeout(() => controller.abort(), 50);
try {
await delay(10_000, { signal: controller.signal });
} catch {
console.log("sleep canceled after 50ms"); // sleep canceled after 50ms
}A pending timer keeps the process alive. The persistent option releases it, so a long delay will not stop a finished program from exiting.
await delay(0, { persistent: false });Run this example locally using the Deno CLI:
deno run https://docs.deno.com/examples/scripts/sleep_delay.ts