I'm getting this error when I try to await outside the async function Can not use keyword 'await' outside an async function and I wanted to know hich approach should I use to fix the issue? Thanks in advance.
Here is my code:
async addCoursToClassYear() { setTimeout(() => { 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); } }); }) },
setTimeout(async () => {- That should fix it.setTimeoutwithout delay? It will place callback in event queue, yes, but if the callback is already asynchronous what is the point in delaying it's invocation by 4ms minimum?setTimoutis because I want to have the submit button to load and be disabled for 2 seconds before submitting the request. To avoid double clicking by user.dispatch. it looks no reason to use async/await in the function.