async ... await

promise写法

function runAsync1(){
            var p = new Promise(function(resolve, reject){
                //做一些异步操作
                setTimeout(function(){
                    console.log('异步任务1执行完成');
                    resolve('随便什么数据1');
                }, 1000);
            });
            return p;
        }
        function runAsync2(){
            var p = new Promise(function(resolve, reject){
                //做一些异步操作
                setTimeout(function(){
                    console.log('异步任务2执行完成');
                    resolve('随便什么数据2');
                }, 2000);
            });
            return p;
        }
        function runAsync3(){
            var p = new Promise(function(resolve, reject){
                //做一些异步操作
                setTimeout(function(){
                    console.log('异步任务3执行完成');
                    resolve('随便什么数据3');
                }, 2000);
            });
            return p;
        }

        runAsync1()
                .then(function(data){
                    console.log(data);
                    return runAsync2();
                })
                .then(function(data){
                    console.log(data);
                    return runAsync3();
                })
                .then(function(data){
                    console.log(data);
                });

1. 包装多个promise对象 

2. 多个异步操作 要在回调里 return下一个promise对象  ,操作多了,回调也是有点麻烦 

==============

async 写法

用同步的方式 写异步操作

获取部门数据时,必须是return 异步对象

如果return res = this.$ajax...     这样会有问题 取值 undefined

getDeptData(){ //获取部门数据
				return this.$ajax({
					url: this.$ajax.apiUrl("/sys/dept/list"),
					method: "get",
					params: this.$ajax.adornParams()
				});
			},

			getRolesData(){ //获取角色数据
				return  this.$ajax({
					url: this.$ajax.apiUrl("/sys/role/all"),
					method: "get",
					params: this.$ajax.adornParams()
				});

			},

			async init(id){
				try{
					let res_dept = await this.getDeptData();
					let res_role = await this.getRolesData();

					this.deptList = this.$tool.treeDataTranslate(res_dept.data);
					this.roleList = res_role.data;
					this.dataForm.id = id;
					this.isEdit=false,

					this.visible = true;
					this.$nextTick(() => {
						this.$refs["dataForm"].resetFields();
					});


					if (!id) {// 新增
						this.dataForm.deptName = null;
						console.log('新增', this.dataForm)
					} else {// 修改
						this.isEdit = true;

						this.$ajax({
							url: this.$ajax.apiUrl(`/sys/user/detail`),
							method: "get",
							params: this.$ajax.adornParams({
								id: this.dataForm.id
							})
						}).then(({data}) => {
							console.log('详情', data);
							this.dataForm.deptId = data.deptId;
							this.dataForm.loginName = data.loginName;
							this.dataForm.phoneNumber = data.phoneNumber;
							this.dataForm.remark = data.remark;
							this.dataForm.roleId = data.roleId;
							this.dataForm.status = data.status;
							this.dataForm.userName = data.userName;

							this.dataForm.id = id;
							this.deptListTreeSetCurrentNode();

						});
					}
				} catch(err){
					console.log(err)
				}
			},

 

转载于:https://my.oschina.net/u/560237/blog/3082357

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值