[Vuejs]-Can not use keyword 'await' outside an async function in VUEJS

1👍

Here, put async in just the function in which you using await.

async addCoursToClassYear() {
  setTimeout(async () => {
    this.loading = false;
    await this.$store.dispatch("academicYear/addCoursToClassYear", {
        ...this.form,
        ...this.singleYear
      })
      .then(() => {
        this.handleSuccess();
        this.closeModal();
      })
      .catch(error => {
        if (error.response.status === 422) {
          this.serverValidation.setMessages(error.response.data.errors);
        } else {
          this.handleError(error);
        }
      });
  })
},

2👍

You are using await inside setTimeout. So, you need to make async setTimeout function

async addCoursToClassYear() {
  setTimeout(async() => {
    this.loading = false;
    await this.$store.dispatch("academicYear/addCoursToClassYear", {
        ...this.form,
        ...this.singleYear
      })
      .then(() => {
        this.handleSuccess();
        this.closeModal();
      })
      .catch(error => {
        if (error.response.status === 422) {
          this.serverValidation.setMessages(error.response.data.errors);
        } else {
          this.handleError(error);
        }
      });
  })
},

I changed

setTimeout(() => {

To

setTimeout(async() => {

Leave a comment