美文网首页
promise.all的实际运用

promise.all的实际运用

作者: 萤火kin | 来源:发表于2022-03-28 14:41 被阅读0次

promise.all的实际运用

表单校验时使用

  • 页面存在多个表单验证,所有表单验证通过后,才进行操作
saveDetail () {
            const infoForm = this.$refs['merchantInfoForm'].validate();
            const settleVal = this.$refs['settleForm'].validate();
            Promise.all([infoForm, settleVal]).then(res => {
                if (res.every(item => item)) {
                    this.SaveMerchantsDetails();
                }
            })
},

接口请求时使用

  • 使用场景:首先请求字典项接口,当所有字典项接口请求完毕,才开始请求信息接口
created() {
    this.getMasterDicts();
},
methods: {
    getMasterDicts() {
      let p1 = new Promise((resolve, reject) => {
        // 证件类型
        getMasterDicts("card_type").then((res) => {
          if (res.code === 200) {
            this.cardTypeOptions = res.data || [];
            this.cardTypeOptions.map((item) => {
              item.name = item.dictLabel;
            });
            resolve();
          }
        });
      });
      let p2 = new Promise((resolve, reject) => {
        // 学历
        getMasterDicts("education").then((res) => {
          if (res.code === 200) {
            this.educationOptions = res.data || [];
            this.educationOptions.map((item) => {
              item.name = item.dictLabel;
            });
            resolve();
          }
        });
      });
      let p3 = new Promise((resolve, reject) => {
        // 学位
        getMasterDicts("degree").then((res) => {
          if (res.code === 200) {
            this.degreeOptions = res.data || [];
            this.degreeOptions.map((item) => {
              item.name = item.dictLabel;
            });
            resolve();
          }
        });
      });
      let p4 = new Promise((resolve, reject) => {
        // 职称
        getMasterDicts("certificate").then((res) => {
          if (res.code === 200) {
            this.certificateOptions = res.data || [];
            this.certificateOptions.map((item) => {
              item.name = item.dictLabel;
            });
            resolve();
          }
        });
      });
      let p5 = new Promise((resolve, reject) => {
        // 执业类别
        getMasterDicts("project_property").then((res) => {
          if (res.code === 200) {
            this.projectPropertyOptions = res.data || [];
            resolve();
          }
        });
      });
      Promise.all([p1, p2, p3, p4, p5]).then((res) => {
        this.getUserInfo();
      });
    },
}

相关文章

  • 手写代码系列

    深拷贝 html转码 手写promise.all 有哪些实现数组扁平化的方法 原生js 运用flat方法 redu...

  • VB的实际运用

    导师林海峰 整体自然疗法 维生素B是一个族群,包括了维生素B1、B2、B3、B5、B6、B12、叶酸、胆碱、肌醇...

  • requestFeature的实际运用

    设置Activity悬浮 通过在styles.xml中设置windowIsFloating属性实现Activity...

  • RunLoop的实际运用

    .h代码: .m代码:

  • runtime实际运用

    记录一下runtime实际运用,更加了解一下runtime 一、为所有的类添加打印私有属性的功能 为NSObjec...

  • Runtime 实际运用

    runloop和线程一一对应runloop包含多个mode, mode包含多个 mode item(sources...

  • Promise.allSettled,ES2020新特性对于Pr

    Promise.all方法 使用promise.all如果有一个对象reject了,那么promise.all就会...

  • iOS | mutableCopy的实际运用

    这是一篇发现问题和解决问题的笔记。 遇到的问题 每次上拉加载更多完成执行[self.tableView reloa...

  • 关于Appledoc的实际运用

    Appledoc 的安装和基本使用,可以参考此文章 这里记录一个操作,一般情况下。我们只需要某个目录的class需...

  • 设计模式的实际运用

    做计划时方法论很重要,当要计划写代码时,可以想想是否符合已有的设计模式呢。我通过思考自己多年的项目经历,总结下用到...

网友评论

      本文标题:promise.all的实际运用

      本文链接:https://www.haomeiwen.com/subject/qnxpjrtx.html