Promise.all

46 阅读1分钟

Promise.all

Promise.myAll = function(list){
  const results = []
  let count = 0
  return new Promise((resolve, reject) => {
    list.map((item, index) => {
      item.then(result => {
        results[index] = result
        count += 1
        if(count >= list.length){resolve(results)}
      }, reason => reject(reason))
    })
  })
}