Execute batch of promises in series. Once Promise.all is done go to the next batch

后端 未结 6 1866
星月不相逢
星月不相逢 2021-02-19 15:57

I have an array that contains an array of promises, and each inner array could have either 4k, 2k or 500 promises.

In total there are around 60k promises and I may test

6条回答
  •  孤街浪徒
    2021-02-19 16:43

    In addition, if original array is not of promises but of objects that should be processed, batch processing can be done without an external dependency using combination of Array.prototype.map(), Array.prototype.slice() and Promise.all():

    // Main batch parallelization function.
    function batch(tasks, pstart, atonce, runner, pos) {
      if (!pos) pos = 0;
      if (pos >= tasks.length) return pstart;
      var p = pstart.then(function() {
        output('Batch:', pos / atonce + 1);
        return Promise.all(tasks.slice(pos, pos + atonce).map(function(task) {
          return runner(task);
        }));
      });
      return batch(tasks, p, atonce, runner, pos + atonce);
    }
    
    // Output function for the example
    function output() {
      document.getElementById("result").innerHTML += Array.prototype.slice.call(arguments).join(' ') + "
    "; window.scrollTo(0, document.body.scrollHeight); } /* * Example code. * Note: Task runner should return Promise. */ function taskrunner(task) { return new Promise(function(resolve, reject) { setTimeout(function() { output('Processed:', task.text, 'Delay:', task.delay); resolve(); }, task.delay); }); } var taskarray = []; function populatetasks(size) { taskarray = []; for (var i = 0; i < size; i++) { taskarray.push({ delay: 500 + Math.ceil(Math.random() * 50) * 10, text: 'Item ' + (i + 1) }); } } function clean() { document.getElementById("result").innerHTML = ''; } var init = Promise.resolve(); function start() { var bsize = parseInt(document.getElementById("batchsize").value, 10), tsize = parseInt(document.getElementById("taskssize").value, 10); populatetasks(tsize); init = batch(taskarray.slice() /*tasks array*/ , init /*starting promise*/ , bsize /*batch size*/ , taskrunner /*task runner*/ ); }
    
     Batch size: 
     Tasks: 
    
    

提交回复
热议问题