[Vuejs]-Async/await syntax sometimes seemed not working

0👍

You need to make mounted as an async.

async mounted() {
  await this.A();
  await this.B();
}

0👍

A returns a promise. If your mounted() function isn’t an async function that is capable of using await, then you’ll have to use then.

this.A().then(()=>{
   this.B();
});

(Sorry, the above is typescript. You can figure out how to do the javascript… it’s not my specialty at all.)

0👍

You need to tell the mounted method to await the first method before calling the second method. Remember to pretend mounted() with async.

async mounted() {
  await this.A();
  this.B();
}

0👍

mounted() {
  Promise.all([this.a(), this.b()])
         .then((res) => {
               console.log(res)
         })
}

async A() {
  const result = await this.$api...
  return result
}

async B() {
  if(this.result) {
         const data = await this.$another1 api...
         return data
  } else {
          const data = await this.$another2 api...
          return data
  }
}

Leave a comment