Promise.all

发布于 2023-09-25  190 次阅读


vue_等多个请求执行完再执行下一个方法Promise.all用法

getData1() {
    return new Promise((resolve, reject) => {
        request1(requestParams).then((res) => {//接口1
          xxx
          resolve(res);
        }).catch(e =>{
          reject(e)
        })
    });
},
getData2() {
    return new Promise((resolve, reject) => {
        request2(requestParams).then((res) => {//接口2
          xxx
          resolve(res);
        }).catch(e =>{
          reject(e)
        })
    });
},
getData() {
    this.addLoading()//开始loading
    Promise.all([
      this.getData1(),
      this.getData2()
      ]).then(res => {
        console.log(res);
        setTimeout(() => {
          this.hideLoading()//延时结束loading
        }, 800);
    })
}

图片上传链接https://www.zhihu.com/question/313595404?utm_id=0


一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。