promise 并行

//省略代码
function limitLoad(urls, handler, limit) {
  // 对数组做一个拷贝
  const sequence = [].concat(urls)
  let promises = [];

  //并发请求到最大数
  promises = sequence.splice(0, limit).map((url, index) => {
      // 这里返回的 index 是任务在 promises 的脚标,
      //用于在 Promise.race 之后找到完成的任务脚标
      return handler(url).then(() => {
          return index
      });
  });

  let p = Promise.race(promises);
  for (let i = 0; i < sequence.length; i++) {
      p = p.then((res) => {
          promises[res] = handler(sequence[i]).then(() => {
              return res
          });
          return Promise.race(promises)
      })
  }
}


var urls = [
  'https://www.kkkk1000.com/images/getImgData/getImgDatadata.jpg', 
  'https://www.kkkk1000.com/images/getImgData/gray.gif', 
  'https://www.kkkk1000.com/images/getImgData/Particle.gif', 
  'https://www.kkkk1000.com/images/getImgData/arithmetic.png', 
  'https://www.kkkk1000.com/images/getImgData/arithmetic2.gif', 
  'https://www.kkkk1000.com/images/getImgData/getImgDataError.jpg', 
  'https://www.kkkk1000.com/images/getImgData/arithmetic.gif', 
  'https://www.kkkk1000.com/images/wxQrCode2.png'
];

function loadImg(url) {
  return new Promise((resolve, reject) => {
      const img = new Image()
      img.onload = function () {
          console.log('一张图片加载完成');
          resolve();
      }
      img.onerror = reject
      img.src = url
  })
};
limitLoad(urls, loadImg, 3)

 

你可能感兴趣的:(promise 并行)