difference between 'util.promisify(setTimeout)' and 'ms => new Promise(resolve => setTimeout(resolve, ms))'

前端 未结 3 460
北荒
北荒 2021-01-21 11:47

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)
         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-21 12:13

    You know that this right here works:

    const {promisify} = require('util');
    const sleep = promisify(setTimeout);
    
    ;(async () => {
    
      const ts = Date.now()
    
      await sleep(5000)
    
      console.log(Date.now()-ts)
    
    })();
    

    This works fine, why not go with it and use it???

提交回复
热议问题