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.
If you're using Bluebird, you can also use Promise.delay: http://bluebirdjs.com/docs/api/promise.delay.html