Skip to content

Instantly share code, notes, and snippets.

@berstend
Last active July 16, 2023 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berstend/67579596df84cbee84a6200999a969da to your computer and use it in GitHub Desktop.
Save berstend/67579596df84cbee84a6200999a969da to your computer and use it in GitHub Desktop.
The best ES6 Promise.delay functions

Hello future self, here's the TypeScript version for your copy/pasting pleasure:

const delay = (ms: number) => new Promise(_ => setTimeout(_, ms))

and your favorite JS version:

const delay = ms => new Promise(_ => setTimeout(_, ms))

and a Node.js (>= v16) only variant:

import { setTimeout } from "timers/promises"
await setTimeout(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment