[Vuejs]-Can I set a variable value while awaiting on an async function?

2👍

Maybe just set the variable before and after the await call?

this.IsUploading = true;

await fileUpload;

this.IsUploading = false;

1👍

Here are some cases, that you can use.

  1. I think this is the best and the most correct way, it’ll call your function only once, maybe, the problem is in another thing?
async submitForm() {
  const FormBody = new FormData();

  await fileUpload();

  this.$axios.post('/api', FormBody)
}
  1. You can make set your value to true and wait until fileUpload() returns something:
async submitForm() {
  const FormBody = new FormData();
  this.IsUploading = true;

  if (await fileUpload()) this.IsUploading = false;

  this.$axios.post('/api', FormBody)
}
  1. Or just change it right away after getting respond from fileUpload(), as told Jack Bashford:
async submitForm() {
  const FormBody = new FormData();
  this.IsUploading = true;

  await fileUpload();
  this.IsUploading = false

  this.$axios.post('/api', FormBody)
}

Leave a comment