Next.js Discord

Discord Forum

setTimeout and async/await, good idea?

Unanswered
Virginia's Warbler posted this in #help-forum
Open in Discord
Virginia's WarblerOP
This is more of a JS question, but is it a good idea or an anti-pattern to have something like below?
setTimeout(async () => {
  await blaBla();
}, 500);

7 Replies

there's also
import { setTimeout } from 'node:timers/promises'
await setTimeout(1000)
and this:
import { promisify } from "node:util";
const wait = promisify(setTimeout)
await wait(1000)
oh nice, not applicable to the browser but still nice
@@ts-ignore there's also ts import { setTimeout } from 'node:timers/promises' await setTimeout(1000)
Asian black bear
I wonder who showed you this 😏
Virginia's WarblerOP
Thank for for all those ideas.
But would there be "catastrophic consequences" if I write:
setTimeout(async () => {
  await blaBla();
}, 500);
not really, no, because it works...

but it's like combining .then .catch with async/await. it's unnecessary and just open up your code to future bugs