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.

Edit on GitHub

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 later

The standard library provides a delay helper, with extras.

import { delay } from "jsr:@ende/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 Ende CLI:

ende run https://docs.ende.sh/examples/scripts/sleep_delay.ts

Additional resources