《手写Promise.All》

38 阅读1分钟

//list为Promise数组 
Promise.myAll = function(list) {
    let 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) ) 
           
        })
    } )
}