let promise wait a couple of seconds before return

后端 未结 4 1848
迷失自我
迷失自我 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:30

    A different approach - useful if you want to do this sort of thing in many places

    This bit is done once

    Promise.prototype.thenWait = function thenWait(time) {
        return this.then(result => new Promise(resolve => setTimeout(resolve, time, result)));
    };
    

    Then you can use it, like this usage for your example, anywhere

    apiGetLoggedInUser.thenWait(3000).then(user => sendMessage(user.name));
    

提交回复
热议问题