promise.all是一个静态方法,只会接受成功的结果
let p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(111)
}, 1000)
})
let p2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(222)
}, 2000)
})
Promise.all([p1, p2]).then(res => {
console.log(res)//[111,222]
})
myPromiseAll (lists) {
return new Promise((resolve, reject) => {
let resArr = []
let num = 0
lists.forEach(item => {
item.then(res => {
resArr.push(res)
num++
if (num == lists.length) {
resolve(resArr)
}
})
})
})
}
this.myPromiseAll([p1, p2]).then(res => {
console.log(res)//[111,222]
})