We have 3 methods:
//nodejs const _sleep1 = require('util').promisify(setTimeout) //nodejs const _sleep2 = setTimeout[Object.getOwnPropertySymbols(setTimeout)[0]] //js const _sleep3 = m => new Promise(r => setTimeout(r, m))
Main function:
async function sleep(fn, ...args) { if(args.length==0){ throw new Error("Require at least 2 parameters") } let timeout = args.pop() await _sleep1(timeout) //can change to _sleep2, _sleep3 return fn(...args) }
Example:
!(async ()=>{ await sleep(()=>{ console.log('Hello') },2000) })()