Reference: Typescript asynchronous sleep that returns a promise

I want some code to execute after a delay. I want to do this with promises, in TypeScript, asynchronously. Apparently this is hard. Here is the spell:

const sleepPlease: (number) => Promise<void> = 
    promisify( (a, b) => setTimeout(b, a));

const slow: Promise<string> = 
    sleepPlease(500).then(() => “yay finally”);

I imported promisify from “util”. setTimeout is built in, but its arguments are in the wrong order to naturally pass to promisify

1 thought on “Reference: Typescript asynchronous sleep that returns a promise

Comments are closed.

Discover more from Jessitron

Subscribe now to keep reading and get access to the full archive.

Continue reading