Delays between promises in promise chain

后端 未结 7 966
旧巷少年郎
旧巷少年郎 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条回答
  •  旧时难觅i
    2020-12-14 20:56

    The answers are good, but they wait too long since all the answers wait regardless of whether or not the actual operation took more than 50ms already.

    You can use Promise.all for it.

    const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
    let parameterArr = ['a','b','c','d','e','f'];
    parameterArr.reduce(function(promise, item) {
      return promise.then(function(result) {
        return Promise.all([delay(50), myPromise(item)]);
      });
    }, Promise.resolve());
    

提交回复
热议问题