function

sleep

function sleep(
ms: number | Date
): Promise<void>;

Returns a Promise that resolves after the given number of milliseconds, or at the given Date. Like setTimeout, except it returns a Promise.

@param ms

milliseconds to wait before resolving the promise. This is a minimum; it may take longer. Pass a Date to sleep until that time is reached.

Sleep for 1 second#

import { sleep } from "bun";

await sleep(1000);

Sleep for 10 milliseconds#

await Bun.sleep(10);

Sleep until Date#

const target = new Date();
target.setSeconds(target.getSeconds() + 1);
await Bun.sleep(target);

Internally, Bun.sleep is the equivalent of

await new Promise((resolve) => setTimeout(resolve, ms));

Bun.sleep and the imported sleep function are interchangeable.