两个异步请求成功后执行第三个异步函数

在公司的项目中有个需求是 每个公司只能有一个主账号,无数个子账号,每个子账号都可以设置为主账号,当设置为主账号的时候,以前的主账号就变为子账号
需要调用三个接口 1. 把当前的主账号设置为子账号 2.把当前的子账号设置为主账号 3. 重新获取到列表中的数据
使用的是Promise.all方法实现


 contactSet(id) {
     console.log(this.mainAccountCompanyId);
     console.log(this.mainAccountBoolean);
     console.log(this.mainAccountName);
     if (this.mainAccountBoolean) {
       this.$confirm(
         `该公司已有联系人为${this.mainAccountName}主账号,确认将该账号设置为主账号?`,
         "提示",
         {
           confirmButtonText: "确认",
           cancelButtonText: "取消",
           type: "warning"
         }
       )
         .then(() => {
           Promise.all([
   						//先把当前的主账号设置为子账号
             setContactStatus({
               user_id: this.mainAccountCompanyId,
               account_type: "2"
             }),
   						//再把子账号设置为主账号
             setContactStatus({
               user_id: id,
               account_type: "1"
             })
           ]).then(res => {
   						//再次调取方法,获取到新的数据
             this.getContactList();
           });
         })
         .catch(() => {});
     } else {
   			//没有主账号的时候直接设置
       this.$confirm("确认将该账号设为主账号?", "提示", {
         confirmButtonText: "确认",
         cancelButtonText: "取消",
         type: "warning"
       })
         .then(() => {
           let params = {
             user_id: id,
             account_type: "1"
           };
           console.log(params);
           setContactStatus(params).then(res => {
             console.log(res);
           });
           this.getContactList();
         })
         .catch(() => {});
     }
   },


以上就是Promise.all的用法

原文地址:https://www.cnblogs.com/my466879168/p/12213424.html