zoukankan      html  css  js  c++  java
  • 两个异步请求成功后执行第三个异步函数

    在公司的项目中有个需求是 每个公司只能有一个主账号,无数个子账号,每个子账号都可以设置为主账号,当设置为主账号的时候,以前的主账号就变为子账号
    需要调用三个接口 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的用法

  • 相关阅读:
    操作系统-多进程图像
    025.Kubernetes掌握Service-SVC基础使用
    Linux常用查看版本指令
    使用动态SQL处理table_name作为输入参数的存储过程(MySQL)
    INTERVAL 用法 mysql
    sql server编写archive通用模板脚本实现自动分批删除数据【填空式编程】
    docker部署redis集群
    Ubuntu1804下安装Gitab
    Bash脚本编程学习笔记06:条件结构体
    KVM虚拟化基础
  • 原文地址:https://www.cnblogs.com/my466879168/p/12213424.html
Copyright © 2011-2022 走看看