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;
}
});
Unresolved variable or type await.Highlights functions that were possibly intended to be async but are missing the async modifier.How to use await in =>?Give me some help.Thanks
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;
}
});
Unresolved variable or type await.Highlights functions that were possibly intended to be async but are missing the async modifier.How to use await in =>?Give me some help.Thanks
Share Improve this question edited Jun 19, 2018 at 10:10 woki asked Jun 19, 2018 at 9:37 wokiwoki 951 gold badge1 silver badge7 bronze badges3 Answers
Reset to default 13The async
keyword must be used for the function that USES await in its body.
So, move async
from checkDriver
to addValidate
:
checkDriver(mobile) {
// ...
}
async addValidate() {
// ...
let status = await this.checkDriver(this.form.mobile);
}
Also, the checkDriver
method should return a promise. So change the contents of checkDriver
to:
checkDriver(mobile) {
return this.$axios({
url: "xxx",
method: "POST",
data: {
mobile: mobile
}
})
}
and the data returned from the Promise (axios in this case) will be assigned to status
in addValidate
Then if you want to handle errors, you should wrap the await
call in a try/catch block.
addValidate() {
this.$refs['form'].validate( async (valid) => {
if (valid) {
let status = await this.checkDriver(this.form.mobile);
console.log(status);
} else {
return false;
}
});
You can add the missing async
keyword beside the parameter
You must make addValidate()
async, like async addValidate() { /* ... */ }
. You can only use await
in a function that's marked async
.