[Vuejs]-How to use async/await inside promise – Vue

4👍

You can’t use the await keyword directly inside a function that isn’t async. You are using the await keyword directly inside the anonymous function which is then‘s parameter; this anonymous function isn’t async, so you have a syntax error.

Here’s how I would rewrite this function:

async sendEmail() {
  this.isActive = true
  let result = await this.$validator.validate();
  if (result) await axios.post('url', data);
  this.isActive = false;
}

I find it’s cleaner to avoid using then when you’re already inside an async function.

Btw, I’m not sure where your data variable is defined.

Leave a comment