Sleep in Node without writing a timeout function

Node ships a promise-based setTimeout, so the little helper from my old snippet isn't needed there.

SNP 036 #B63 Filed js node

A while ago I wrote a tiny timeout helper for async functions. In Node you don't need it:

javascript
import { setTimeout as sleep } from 'node:timers/promises'

await sleep(2000)

It also takes a value to resolve with and an AbortSignal, so a sleep can be cancelled:

javascript
const controller = new AbortController()
sleep(5000, 'done', { signal: controller.signal }).catch(() => {})
controller.abort()

In the browser you still need the new Promise((resolve) => setTimeout(resolve, ms)) version.