promise ALL
2019-08-19 本文已影响0人
织雪纱奈
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)
}
})
}