Delay chained promise

后端 未结 3 1071
我在风中等你
我在风中等你 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:38

    A slightly improved version of jfriend00's approach avoids needing the pass the resolved value like this:

    function delay(t) {
      return (val) => {
        return new Promise((resolve, reject) => {
          setTimeout(() => resolve(val), t)
        })
      }
    }
    

    can be used like this:

    myService.new(a)
      .then(delay(60000))
      .then(function(temp) {
        return myService.get(a, temp);
      }).then(function (b) {
        console.log(b);
      });
    

提交回复
热议问题