Promise的几个扩展API总结
Promise的几个扩展API总结
1. Promise.none
描述:
和 Promise.all 相反,当所有的promise被拒绝之后,none方法执行完成的决议,如果存在一个promise执行完成的决议,none方法则执行拒绝
code:
Promise.none = function(promises) { return Promise.all(promises.map(promise => { return new Promise((resolve, reject) => { // Promise.all里边的所有promise实例反过来就好了 return Promise.resolve(promise).then(reject, resolve) }) })) }
const promisesForNoneTest1= [ Promise.reject('1'), Promise.reject('2'), Promise.resolve('3'), Promise.reject('4'), ] Promise.none(promisesForNoneTest1).then(res => { debugger }, res => { debugger // 执行到此 }) const promisesForNoneTest2= [ Promise.reject('1'), Promise.reject('2'), Promise.reject('3'), Promise.reject('4'), ] Promise.none(promisesForNoneTest2).then(res => { debugger // 执行到此 }, res => { debugger })
2. Promise.any
描述:
忽略被拒绝的promise,只需要有一个完成的promise,any方法就执行完成操作,如果全部的promise都被拒绝,any方法执行拒绝操作
code:
Promise.any = function(promises) { const result = [] return Promise.all(promises.map(promise => { // 控制Promise.all处理的所有的promise都执行reslove决议 return Promise.resolve(promise).then(res => { // 但是只记录实际上决议为resolve的结果值 result.push(res) }, () => { // 防止穿透,这里可以进行拒绝信息的返回 }) })).then(() => { return new Promise((resolve, reject) => { if (result.length > 0) resolve(result) else reject(result) }) }) }
const promisesForAnyTest1= [ Promise.reject('1'), Promise.resolve('2'), Promise.reject('3'), Promise.resolve('4'), Promise.resolve('5'), ] Promise.any(promisesForAnyTest1).then(res => { debugger // 执行到此,res 为 ['2', '4', '5'] }, res => { debugger }) const promisesForAnyTest2= [ Promise.reject('1'), Promise.reject('2'), Promise.reject('3'), Promise.reject('4'), Promise.reject('5'), ] Promise.any(promisesForAnyTest2).then(res => { debugger }, res => { debugger // 执行到此 })
3. Promise.first
描述:
类似race,但是只要有一个promise决议为完成(忽略前边被拒绝的promise),就忽略后边的promise
code:
Promise.first = function(promises) { return new Promise((resolve, reject) => { let rejectNum = 0 promises.forEach(promise => { // 如果当前 promise 决议为reslove,那就直接执行"根promise"的resolve // 否则去记录到拒绝的promise中,然后判断全部的promise拒绝了,执行"根promise"的reject Promise.resolve(promise).then(resolve, () => { if (++rejectNum === promises.length) { // 这里可以控制reject返回的信息 reject() } }) }) }) }
const promisesForFirstTest1= [ Promise.reject('1'), Promise.resolve('2'), Promise.reject('3'), Promise.resolve('4'), ] Promise.first(promisesForFirstTest1).then(res => { debugger // 执行到此,res 为 '2' }, res => { debugger }) const promisesForFirstTest2= [ Promise.reject('1'), Promise.resolve('2'), Promise.reject('3'), Promise.resolve('4'), ] Promise.first(promisesForFirstTest2).then(res => { debugger }, res => { debugger // 执行到此 })
4. Promise.map
描述:
在第二个方法 any 中,用到了Promise.all的方法,里边使用Array.prototype.map方法处理了所有的的promise,当前这个Promise.map方法则希望把他俩弄到一起
Promise.map方法希望实现一个处理批量异步操作的并行迭代方法,本质上是利用Promise.all进行二次封装
code:
Promise.map = function(promises, resolveCallback, rejectCallback) { return Promise.all(promises.map(promise => { return Promise.resolve(promise).then(result => { return resolveCallback(result) }, error => { return Promise.reject(rejectCallback(error)) }) })) }
const promisesForMapTest1= [ Promise.resolve('1'), Promise.resolve('2'), Promise.resolve('3'), Promise.resolve('4'), ] Promise.map(promisesForMapTest1, result => { return result * 100 }, result => { return result }).then(res => { debugger // 执行到此,res为 [100, 200, 300, 400] }, res => { debugger }) const promisesForMapTest2= [ Promise.resolve('1'), Promise.resolve('2'), Promise.reject('3'), Promise.resolve('4'), ] Promise.map(promisesForMapTest2, result => { return result * 100 }, result => { return result }).then(res => { debugger }, res => { debugger // 执行到此,res为 "3" })
相关推荐
nmgxzm00 2020-11-10
xixixi 2020-11-11
88254251 2020-11-01
MarukoMa 2020-09-02
88234852 2020-09-15
陈旭阳 2020-08-31
whynotgonow 2020-08-19
前端开发Kingcean 2020-07-30
whynotgonow 2020-07-29
bowean 2020-07-08
前端开发Kingcean 2020-07-08
88520191 2020-07-05
前端开发Kingcean 2020-06-27
88481456 2020-06-18
whynotgonow 2020-06-16
88520191 2020-06-13
88520191 2020-06-13
89500297 2020-06-13