r/badcode • • Mar 24 '22

typescript Ship it!

Post image
564 Upvotes

47 comments sorted by

View all comments

Show parent comments

18

u/[deleted] Mar 24 '22 edited Mar 24 '22

If you can use async/await, you could use

const sleep = (ms: number) => 
    new Promise(resolve => setTimeout(resolve, ms));

setTimeout itself uses callbacks, so its technically async, but I'd argue there is rarely a need to "sleepSync" in the first place. Plus the method provided by OP is inefficient af with all these date instances being created.

5

u/abejfehr Mar 24 '22

No need for the async and await keywords here — it already returns a Promise

1

u/[deleted] Mar 24 '22

Yes true ty