![](/img/trans.png)
[英]How do I async await multiple promises in parallel? [Syntax Error: await is a reserved word]
[英]vue:Syntax Error: await is a reserved word
async checkDriver(mobile) {
this.$axios({
url: "xxx",
method: "POST",
data: {
mobile: mobile
}
}).then(res => {
console.log("========="+res.status);
return res.data.status;
}).catch(error => {
this.$message.error(error.toString());
return -1;
});
},
addValidate() {
this.$refs['form'].validate((valid) => {
if (valid) {
let status = await this.checkDriver(this.form.mobile);
console.log(status);
} else {
return false;
}
});
未解析的變量或類型 await.Highlights 可能旨在異步但缺少 async 修飾符的函數。如何在 => 中使用 await?給我一些幫助。謝謝
async
關鍵字必須用於在其主體中使用 await 的函數。
因此,將async
從checkDriver
移動到addValidate
:
checkDriver(mobile) {
// ...
}
async addValidate() {
// ...
let status = await this.checkDriver(this.form.mobile);
}
此外, checkDriver
方法應該返回一個承諾。 因此將checkDriver
的內容checkDriver
為:
checkDriver(mobile) {
return this.$axios({
url: "xxx",
method: "POST",
data: {
mobile: mobile
}
})
}
並且從 Promise(在本例中為 axios)返回的數據將分配給addValidate
status
然后,如果您想處理錯誤,您應該將await
調用包裝在 try/catch 塊中。
addValidate() { this.$refs['form'].validate( async (valid) => { if (valid) { let status = await this.checkDriver(this.form.mobile); console.log(status); } else { return false; } });
您可以在參數旁邊添加缺少的async
關鍵字
您必須使addValidate()
異步,例如async addValidate() { /* ... */ }
。 您只能在標記為async
的函數中使用await
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.