Promise.all和Promise.race实现

Promise.all=(proArr) =>{
    return new Promise((resolve, reject) => {
        let retsult = []
        let count = 0
        for (let i = 0; i < proArr.length; i++) {
            proArr[i].then(data => {
                retsult[i]=data
                if(++count===proArr.length){
                    resolve(retsult)
                }
            }, reject)
        }
    })
}
Promise.race=(promiseAry)=> {
    return new Promise((resolve, reject) => {
      for (let i = 0; i < promiseAry.length; i++) {
        promiseAry[i].then(resolve, reject)
      }
    })
  }

你可能感兴趣的:(javascript)