I am trying to use the new async features and I hope solving my problem will help others in the future. This is my code which is working:
async function as
Since Node 7.6, you can combine the functions promisify
function from the utils module with setTimeout()
.
const sleep = require('util').promisify(setTimeout)
const sleep = m => new Promise(r => setTimeout(r, m))
(async () => {
console.time("Slept for")
await sleep(3000)
console.timeEnd("Slept for")
})()