多个Promise同时执行,等待最后一个返回值

项目所需,还好回忆起了《你不知道的JavaScript 中篇》中有所记载

Promise.all:
Promise.all([x,x,x,x]) x所表示其他promise操作,你可以带如 promise封装的所有东西,比如Ajax
需要注意: 这些带入的promise参数 必须返回 resolve,reject 这些决议,当然如果你用的是第三方插件,可能他里面已经封装过promise,可以不用这么麻烦

      Promise.all([this.getTimes(), this.getTime(), this.getRate()])
        .then(res => {
    
    
          let cumm = JSON.parse(localStorage.getItem('customT'))
          if (cumm.type == 2) {
    
    
            this.isBtn = true;
            this.setSj(cumm.data)
          } else {
    
    
            this.isBtn = false;
          }
        }).catch(error => {
    
    
          alert('获取数据失败')
        })
        return new Promise((resolve, reject) => {
    
    
          var that = this;
          $.ajax({
    
    
            url: ipurl + '',
            type: 'post',
            dataType: "json",
            success: function (res) {
    
    
              resolve()
            },
            error: function () {
    
    
              reject()
            }
          })
        })

猜你喜欢

转载自blog.csdn.net/weixin_43311271/article/details/103324237