DEV Community

Cover image for Wait!
Thomas Rigby
Thomas Rigby

Posted on • Originally published at thomasxbanks.com on

3 3

Wait!

Why the rush…?!

Whether you're faking an API response, introducing deliberate cognitive drain, or simply want to slow things down this utility function has got you, baby! 😎

const delay = ms => new Promise(rv => setTimeout(rv, ms))
Enter fullscreen mode Exit fullscreen mode

Cool! How do I use it?

delay(500).then(() => console.log(`hello world`))
Enter fullscreen mode Exit fullscreen mode

Nice! Does it work with async/await?

Heck, yes, it does!

const functionName = async () => {

  doFirstThing()

  await delay(500)

  doNextThing()
}
Enter fullscreen mode Exit fullscreen mode

See the Pen Delay by thomas×banks (ツ) (@thomasxbanks) on CodePen.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay