await 解决异步请求接口执行先后顺序的问题

一: 示例代码
async
created() {
  // 下拉数据
const keepTypeList = await getToolKeepTypeList()
this.keepTypeList = keepTypeList.data
  const machineSpecList = await getMachineList()
this.machineSpecList = machineSpecList.data

await this.getList();
},

// 数据加载
async getList() {
this.loading = true;
await listData(this.queryParams).then(response => {
const data = response.rows
for(const item of data){
for(const eve of this.machineSpecList){
if(item.tksTmtid == eve.key){
item.tksTmtid = eve.value
}
}
for(const eve of this.keepTypeList){
if(item.tksType == eve.key){
item.tksType = eve.value
}
}
}
this.dataList = data;
this.total = response.total;
this.loading = false;
});
},


这样就会依次向下执行代码

二: 补充
PS: map foreach 等带有回调函数的都是异步函数,尽量少用,可以用for of 代替,因为for of就是一个简单的循环,不会带有回调。
posted @ 2021-11-19 10:11  慕雪琳鸢  阅读(976)  评论(0)    收藏  举报