Environment: node 8.11.x I want use async/await for sleep a while.
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
await sleep(5000)
>
This can be a one-liner: await promisify(setTimeout)(1000)
.
It works because setTimeout has a custom variant for promisify. It does work with node 8.11.
nvm install 8.11 && nvm use 8.11
node < {
// const start = Date.now();
await require('util').promisify(setTimeout)(5000);
// console.log(Date.now() - start);
})()
HEREDOC