Delays between promises in promise chain

后端 未结 7 939
旧巷少年郎
旧巷少年郎 2020-12-14 20:35

Let\'s say I am using the following code to run a couple of promises in series:

let paramerterArr = [\'a\',\'b\',\'c\',\'d\',\'e\',\'f\']
parameterArr.reduce         


        
7条回答
  •  囚心锁ツ
    2020-12-14 20:54

    To get a delay of at least 50ms, use Promise.all:

    function delay(t) {
      return new Promise(function(resolve) {
        setTimeout(resolve, t);
      });
    }
    parameterArr.reduce(function(promise, item) {
      return promise.then(function() {
        return Promise.all([
          mySpecialFunction(item),
          delay(50)
        ]);
      });
    }, Promise.resolve());
    

提交回复
热议问题