手写实现Promise.all 发表于 2020-07-09 | 更新于 2022-09-19 | 分类于 js | 阅读次数: 12345678910111213141516171819202122232425262728293031323334const PromiseAll = (array)=>{ let tempValue = []; let i = 0; return new Promise((resolve,reject)=>{ const next = () => { array[i].then((res)=>{ i++; tempValue.push(res) return i === array.length ? resolve(tempValue):next() }).catch((res)=>{ reject(`reject ${res}`) }) } next(); })}let p1 = Promise.resolve(1);let p2 = Promise.resolve(2);let p3 = Promise.resolve(3);PromiseAll([p1,p2,p3]).then(res=>{ console.log(res) // [1,2,3]})let p4 = Promise.resolve(2);let p5 = Promise.resolve(2);let p6 = Promise.reject(1);PromiseAll([p4,p5,p6]).then(res=>{ console.log(res) // reject 1}) 交个朋友吧 点滴分享,您的支持将极大的鼓励我! 打赏 本文作者: PenZ 本文链接: http://yoursite.com/2020/07/09/手写实现Promise-all/ 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!