bun/prefer-builtin-sleep
Prefer Bun.sleep() to a promise that only waits for setTimeout
has suggestions
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#
example.js1 problem
await new Promise(resolve => setTimeout(resolve, 500))This promise only waits. Use Bun.sleep(ms) or setTimeout(ms) from node:timers/promises.;example.js1 problem
function wait(ms) {
return new Promise(resolve => {This promise only waits. Use Bun.sleep(ms) or setTimeout(ms) from node:timers/promises.
setTimeout(() => resolve(), ms);
});
}example.jsNo problems
await Bun.sleep(500);example.jsNo problems
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:
example.jsNo problems
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.