异步函数async/await

作用

异步函数调用的同时后边的函数也会同时执行

async用法

这里有一个异步函数

async function System(){
      return "yibu";
    }
    console.log(System());
    console.log("执行");

可以看出这里异步方法返回一个已成功的promise对象
在这里插入图片描述
现在把promise对象通过.then方法回调函数

async function System(){
      return "yibu";
    }
    //使用.then指定fulfilled(resolved)状态的指定回调函数
    System().then((value) => {
      console.log(value);
    })
    console.log("执行");

输出结果
在这里插入图片描述

await用法

在async异步函数之中使用,若promise 返回fulfilled(已成功)则await返回promise已成功后的结果,再继续执行async异步函数,若promise 返回rejected(已失败)则await会抛出异常原因

async getHistoryList(){
			let _this = this;
			var nowDate=new Date();
			nowDate.setMonth(nowDate.getMonth()-1);//前一月
			var month=nowDate.getMonth()+1;
			if(month<10)
			{
				month="0"+month;
			}
			var day=nowDate.getDate();
			if(day<10){
				day="0"+day;
			}
			var monthDate=nowDate.getFullYear() + "-" + month + "-" + day;
			await _this.getHistoryVal(_this.temperatureId,60,0,);
			await _this.getHistoryVal(_this.temperatureId,60,1,0,monthDate,monthDate);
			
			for(var i=0;i<_this.cylenderList.length;i++)
			{
				var cylender=_this.cylenderList[i];
				await _this.getHistoryVal(cylender.id,60,2,i);
				await _this.getHistoryVal(cylender.id,60,3,i,monthDate,monthDate);
				if(cylender.PressureMostVal<13.8)
				{
					cylender.color="红色";
					// cylender.year="<2年";
					cylender.src = "/static/warm_red.png"
				}
				else if(cylender.PressureMostVal<14.4)
				{
					cylender.color="黄色";
					// cylender.year="<10年";
					cylender.src = "/static/warm_yellow.png"
				}
				else
				{
					var calculated=cylender.historyPressureMostVal-cylender.PressureMostVal*(273.15+cylender.historyTemperature)/(273.15+cylender.Temperature)
					switch(true)
					{
						case (calculated>0.2):
							cylender.color="红色";
							// cylender.year="<2年";
							cylender.src = "/static/warm_red.png"
							break;
						case (calculated<=0.2&&calculated>0.12):
							cylender.color="黄色";
							// cylender.year="<10年";
							cylender.src = "/static/warm_yellow.png"
							break;	
						default:
							cylender.color="绿色";
							// cylender.year=">=10年";
							cylender.src = "/static/warm_green.png"
							break;
					}
				}
				var cylenderListData_obj = {};
				cylenderListData_obj.name = cylender.name;
				cylenderListData_obj.pressure = cylender.pressure;
				cylenderListData_obj.src = cylender.src;
				// cylenderListData_obj.year = cylender.year;
				_this.cylenderListData.push(cylenderListData_obj);
				
				
			}
			_this.$forceUpdate();
			
			console.log(_this.cylenderList);
			console.log(_this.cylenderListData);
			
		},

api调用 .then 中的用法

this.$u.api
				.authUserLogin({
					username: nameArr.join('/'),
					password: _this.form.password
				})
				.then(async(res) => {
					nameArr = null;
					//当前登录身份区分
					_this.$u.vuex('vuex_type', _this.loginType);
					_this.$u.vuex('vuex_token', res.access_token);
					_this.$u.vuex('vuex_token_obj', res);
					_this.$u.vuex('vuex_has_login', true);

					uni.setStorageSync('vuex_token', res.access_token);

					_this.$u.vuex('vuex_login_obj', _this.form);
					
					await _this.$u.api.comUpdateUserInfo(res.userId);
					_this.appHideLoading();
					_this.showSuccessToast('登录成功');
					_this.$u.route({
						type: 'tab',
						url: 'pages/index/index'
					});

					//运维
					// if (_this.cur == 1) {
					// 	_this.$u.api.comUpdateUserInfo(res.userId);
					// 	_this.$u.route({
					// 		type: 'tab',
					// 		url: 'pages/index/index'
					// 	});
					// } else {
						// _this.$u.api.comUpdateClientUserInfo(res.userId);
						// _this.$u.route({
						// 	type: 'redirect',
						// 	url: 'pages/client/index'
						// });
					// 	_this.$u.route({
					// 		type: 'tab',
					// 		url: 'pages/index/index'
					// 	});
					// }
				})
				.catch(res => {
					_this.appHideLoading();
					nameArr = null;
					_this.$refs.uTips.show({
						title: '登录失败:' + res.data.message,
						type: 'error',
						duration: '1500'
					});
				});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值