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"
  })
    原文作者:yurt
    原文地址: https://segmentfault.com/a/1190000018139168
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞