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