Tuesday, October 24, 2017

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