promise.all = function(promises){
return new Promise((resolve,reject)=>{
let result = []
let cnt = 0
let len = promises.length
for (let i=0; i<len;i++) {
Promise.resolve(promises[i]).then((res)=>{
cnt++
result[i] = res
if(cnt === len) {
resolve(result)
}
},(err)=>{
reject(err)
})
}
})
}
promise.race = function (promises) {
return new Promise((resolve, reject) => {
for (let i = 0; i < promises.length; ++i) {
promises[i].then(resolve, reject)
}
})
}
网友评论