作用
异步函数调用的同时后边的函数也会同时执行
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'
});
});