[Vuejs]-VueJS function with then

3👍

Return the promise in getData, then you can do getData().then():

getData(id){
  return this.$store.dispatch('getData', {
            employeeId: this.employeeId
         }).then(() => {
           if (this.employeeData.length) {
              // here some code...
           }
         });
}
👤Psidom

2👍

Instead of chaining Promises, you could also use ES6s async await, which makes for cleaner more readable code.

async getData(id) {
    await this.$store.dispatch('getData', {
        employeeId: id,
    });

    if(this.employeeDate) //do something
},

async selectEmployee(d) {
    await this.getData(d.employeeId);

    // I need to execute this only after getData has been processed...
    this.$store.dispatch('fetchHistory');
},

Leave a comment