setTimeout and async/await, good idea?
Unanswered
Virginia's Warbler posted this in #help-forum
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
@Virginia's Warbler 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);
i'd prefer
await new Promise(resolve => setTimeout(resolve, 500))
await blaBla()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:
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
but it's like combining .then .catch with async/await. it's unnecessary and just open up your code to future bugs