zoukankan      html  css  js  c++  java
  • 串行执行promise

    Promise提供了Promise.all,Promise.race,Promise.allSettled等多个Promise对象间的运行关系,如果并行运行可以用Promise.all来进行处理,如果要串行运行可以用数组的reduce来进行处理,处理代码如下:

        const serialPromises = function (promises) {
          promises.reduce((prev, next) => prev.then((preVal) => next(preVal)), Promise.resolve());
        }
    

      测试代码

        const fn1 = function (args) {
          console.log('fn1',args)
          return Promise.resolve('111')
        }
        const fn2 = function (args) {
          console.log('fn2',args)
          return Promise.resolve('222')
        }
        const fn3 = function (args) {
          console.log('fn3',args)
          return Promise.resolve('333')
        }
        serialPromises([fn1, fn2, fn3])
    

      测试结果:

    fn1 undefined
    fn2 111
    fn3 222
    

    上面serialPromises有个问题如果有个fn2执行reject那么执行链会发生中断

    测试代码

        const fn1 = function (args) {
          console.log('fn1',args)
          return Promise.resolve('111')
        }
        const fn2 = function (args) {
          console.log('fn2',args)
          return Promise.reject('222')
        }
        const fn3 = function (args) {
          console.log('fn3',args)
          return Promise.resolve('333')
        }
        serialPromises([fn1, fn2, fn3])
    

    测试结果

    fn1 undefined
    fn2 111
    Uncaught (in promise) 222
    

    改进一下

        const serialPromises2 = function (promises) {
          promises.reduce((prev, next) => prev.then(next).catch(next), Promise.resolve());
        }
    

    测试代码

        const fn1 = function (args) {
          console.log('fn1',args)
          return Promise.resolve('111')
        }
        const fn2 = function (args) {
          console.log('fn2',args)
          return Promise.reject('222')
        }
        const fn3 = function (args) {
          console.log('fn3',args)
          return Promise.resolve('333')
        }
        serialPromises2([fn1, fn2, fn3])
    

      测试结果

    fn1 undefined
    fn2 111
    fn2 222
    fn3 222
    

      

    这个方法也有个问题是如果有fn2执行reject,那么那个fn2会执行两次,这是因为p.catch等价于p.then(undefined, onRejected)

    我们再次改进

        const serialPromises3=function(promises){
          const process=function(i,args){
            const curr=promises[i]
            const next=function(res){process(i+1,res)}
            if(curr)curr(args).then(next).catch(next)
          }
          process(0)
        }
    

    测试代码

        const fn1 = function (args) {
          console.log('fn1',args)
          return Promise.resolve('111')
        }
        const fn2 = function (args) {
          console.log('fn2',args)
          return Promise.reject('222')
        }
        const fn3 = function (args) {
          console.log('fn3',args)
          return Promise.resolve('333')
        }
        serialPromises3([fn1, fn2, fn3])
    

      测试结果

    fn1 undefined
    fn2 111
    fn3 222
    

      

    至此问题终于解决了!

  • 相关阅读:
    iReaper
    展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告(turn)
    用C#写ExtJS代码的开源工具extsharp
    如何你是公司的HR,去招聘asp.net程序员,你会对前来面试的人问什么问题。
    ExtJS 3.0 Designer Preview (官方的IDE可视化工具)
    Asp.net ajax、Anthem.net、Ajax pro三大ajax框架那一种使用比较方便?易于配置?
    C#和ASP.net程序员招聘技能要求
    序列化上面创建的Person对象,使其成为一个JSON字符串
    10大加速Ajax开发的框架
    android 解决wifi断线不稳定的问题终极办法
  • 原文地址:https://www.cnblogs.com/zhuxianguo/p/11445952.html
Copyright © 2011-2022 走看看