Loading

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)
                }
            })
        })
     
    })
}
posted @ 2021-01-20 21:24  不吃苦瓜^  阅读(380)  评论(0编辑  收藏  举报