Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
      }
    });

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

like image 416
woki Avatar asked Dec 14 '22 16:12

woki


1 Answers

The 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.

like image 152
skovmand Avatar answered Dec 18 '22 08:12

skovmand