Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

bun/prefer-builtin-sleep

Prefer Bun.sleep() to a promise that only waits for setTimeout

new Promise(resolve => setTimeout(resolve, ms)) takes a moment to read, and all it does is wait. Bun.sleep(ms) does the same thing and says so. setTimeout(ms) from node:timers/promises does too.

--fix-suggestions replaces the promise with Bun.sleep(ms).

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"bun/prefer-builtin-sleep" = "error" # or "warn"

Examples

Reported:

await new Promise(resolve => setTimeout(resolve, 500));
function wait(ms) {
  return new Promise(resolve => {
    setTimeout(() => resolve(), ms);
  });
}

Not reported:

await Bun.sleep(500);
import { setTimeout } from "node:timers/promises";

await setTimeout(500);

A promise that does more than wait isn't reported. This one resolves with a value:

const answer = await new Promise(resolve => setTimeout(() => resolve("timed out"), 500));

When not to use it

Bun.sleep() only exists in Bun, and node:timers/promises doesn't exist in browsers. Turn the rule off for code that also runs in a browser.