Promise.myall=function(proms){
let res,rej
const p =new Promise((resolve,reject)=>{
res=resolve
rej=reject
})
const result=[]
let count=0
let fulfilledCount = 0
for(const key of proms){
const i=count
count++
// Promise.resolve(key)对传入数据转promis
Promise.resolve(key).then(data=>{
// 将成功数据汇总到result
result[i]=data
fulfilledCount++
if(fulfilledCount===count){
res(result)
}
},rej)
}
if(count===0){ // 传入数据为空
res(result)
}
return p
}
手写promise.all
最新推荐文章于 2024-07-29 15:46:22 发布