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就是一个简单的循环,不会带有回调。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM