• Promise.all( ) 的使用


    一、多个函数等待执行

            //初始化权限列表
            initPermission() {
                return new Promise((resolve, reject) => {
                    this.$ajax.get(this.permissionUrl[1], { type: 9 }).then(res => {
                        if (res.code == 200) {
                            console.log(212)
                            this.permissionData = this.$_c.listToTree(res.data, { idKey: 'id', parentKey: 'parent' });
                            resolve(0)//这里一定要加上,否则then和catch都不会执行
                        }
                    })
                })
            },
    
            //初始化操作类型
            initTypeId() {
                return new Promise((resolve, reject) => {
                    this.$ajax.get(this.permissionUrl[2]).then(res => {
                        if (res.code == 200) {
                            //1.selfCheckbox的版本
                            // this.typeData = {
                            //     field: 'typeId',
                            //     isShow: true,
                            //     isInline: true,
                            //     children: res.data
                            // }
                            //2.tree的版本
                            this.newtypeData = this.$_c.listToTree(res.data, { idKey: 'id', parentKey: 'parentCode' })
                            console.log(this.form.typeId)
                            resolve(0)
                            // this.$refs.devTypeTree.setCheckedKeys(this.form.typeId)//设置选中
                        }
                    })
                })
            },

    调用

    mounted() {
            this.initRoleList()//初始化下拉角色列表
            Promise.all([
                this.initPermission(),//初始化权限树
                this.initTypeId()//初始化设备类型树
            ]).then(res => {
                console.log(res)
                this.initCheck()//初始化默认选中
            }).catch(function(){
                console.log(0)
            })
        }

    注意事项

    1.函数里面一定要加上 resolve(0),否则promise.all方法的then和catch都不会执行

    二、如果是单个

    第一种:

    //登陆之前调用退出的接口
    async login() {
                this.ruleForm.date = this.expressTimeSelect == '-' ? (this.expressTimeInput * 60) : this.expressTimeSelect
                if(!this.verify()){return false}
                console.log(document.cookie)
                console.log(cookies.get('access_token'))
                await this.$ajax.get(Api.loginOut, { access_token: cookies.get('access_token') })
                this.startLogin()
            },

    第二种:

    //登录之前掉退出的接口
            async beforeQuit() {
                const res = await this.$ajax.get(Api.loginOut, { access_token: cookies.get('access_token') })
               return res
            },

    登陆前调用

     await this.beforeQuit().then((res) => {})
  • 相关阅读:
    IIS10保存配置文件及导入、导出、备份、还原
    centos7 根分区扩容
    mssqlserver2014安装步骤
    error:class 'socket.error' [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224
    Centos7安装Redis-5.0.3
    Aspose 学习笔记
    Maven学习笔记
    【web性能测试随笔】一、项目介绍及工具
    【Python学习笔记】python开发环境安装部署
    微信小程序中遮罩层滚动穿透问题(view增加一个属性解决)
  • 原文地址:https://www.cnblogs.com/pengfei25/p/13209246.html
Copyright © 2020-2023  润新知