Open Jack-rainbow opened 5 years ago
Promise.myRace = function(promises) {
return new Promise((resolve, reject) => {
for (let item of promises) {
Promise.resolve(item).then(resolve).catch(reject)
}
})
}
const sleep = time => new Promise((resolve) => setTimeout(() => resolve(time), time))
Promise.$race = function(promiseList) {
return new Promise((resolve, reject) => {
promiseList.forEach((p) => {
Promise.resolve(p).then(resolve).catch(reject)
})
})
}
Promise.$race([sleep(3000), sleep(2000), sleep(2500)]).then((res) => {
console.log(res); // 2000
})
Promise.race = (promises = []) => {
return new Promise((resolve, reject) => {
promises.forEach(v=>{
Promise.resolve(v).then(resolve).catch(reject)
})
})
}
设计并实现Promise. race()