Combination of async function + await + setTimeout

前端 未结 12 1533
予麋鹿
予麋鹿 2020-11-22 04:34

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         


        
12条回答
  •  一个人的身影
    2020-11-22 05:19

    Update 2020

    You can await setTimeout with Node.js 15 or above:

    const timersPromises = require('timers/promises');
    
    (async () => {
      const result = await timersPromises.setTimeout(2000, 'resolved')
      // Executed after 2 seconds
      console.log(result); // "resolved"
    })()
    

    Timers Promises API: https://nodejs.org/api/timers.html#timers_timers_promises_api (library already built in Node)


    Note: Stability: 1 - Use of the feature is not recommended in production environments.

提交回复
热议问题