Promise.AllSettled以及Promise.All简单实现
作者:互联网
function PromiseAllSettled(promises) {
const result = []
let len = promises.length
let count = 0
return new Promise((resolve, reject) => {
promises.forEach((ele,index) => { // 每次遍历的判断是否所有的状态改变,如果错误直接reject
const p = ele // 按次序拿出每一个对象
p.then(res => { // resolve操作
result[index] = res
count++
if(count === len ){
resolve(result)
}
}).catch(e => {//如果是promiseAll 有一个被reject就直接返回
result[index] = e
count++
if (count === promises.len) {
reject(result)
}
})
})
})
}
标签:count,index,promises,reject,len,Promise,result,简单,AllSettled 来源: https://www.cnblogs.com/dark-duck/p/14305153.html