let promise wait a couple of seconds before return

后端 未结 4 1849
迷失自我
迷失自我 2021-02-19 09:07

I\'m having a function returning a promise. In this function, we call a third party vender to send some push notification through their server.

it looks like



        
4条回答
  •  后悔当初
    2021-02-19 09:29

    The short version:

    function wait(milliseconds) {
      return new Promise(resolve => setTimeout(resolve, milliseconds));
    }
    

    Example:

    async function myFunc(user) {
      await wait(3000);
    
      sendMessage(user.name);
    }
    

提交回复
热议问题