共计 758 个字符,预计需要花费 2 分钟才能阅读完成。
本系列中的函数都通过测试能够应用,如有错漏,请留言帮忙批改。
Promise.all
Promise.myAll2 = function(arr){let result = []
let count = 0
return new Promise(function(resolve,reject){arr.forEach((value,index)=>{Promise.resolve(arr[index]).then((value)=>{
count++
result.push(value)
if(count === arr.length)
return resolve(result)
},(reason)=>{return reject(reason)
})
})
})
}
let a = new Promise((reslove,reject)=>{reslove(1)
})
let b = new Promise((reslove,reject)=>{reslove(2)
})
let c = new Promise((reslove,reject)=>{reject(3)
})
Promise.myAll2([a,b,c]).then((value)=>{console.log(value)
},(reason)=>{console.log(reason)
})
Promise.race
Promise.myRace = function(arr){return new Promise(function(resolve,reject){arr.forEach((value,index)=>{Promise.resolve(arr[index]).then((value)=>{return resolve(value)
},(reason)=>{return reject(reason)
})
})
})
}
正文完
发表至: javascript
2021-04-22