Delay chained promise

后端 未结 3 1038
我在风中等你
我在风中等你 2020-12-07 05:53

I\'m trying to do the following but it isn\'t working. How can I adjust the code to have a delay between .then and .done?

myServi         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 06:25

    You can create a .delay() function set at Promise.prototype which returns a new Promise having value of original promise.

    Promise.prototype.delay = function(t) {
      return this.then(function(data) {
        return new Promise(function(resolve) {
          setTimeout(resolve, t || 0, data);
        })
      })
    };
    
    Promise.resolve(123)
    .delay(6000)
    .then(function(data) {
      console.log(data)
    });

提交回复
热议问题