Resolve a Promise
after milliseconds. This is like setTimeout except it returns a Promise
.
Symbol
sleep
@param ms
milliseconds to delay resolving the promise. This is a minimum number. It may take longer. If a Date is passed, it will sleep until the Date 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));
As always, you can use Bun.sleep
or the imported sleep
function interchangeably.