0

I have the following method:

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

      this.getData(d.employeeId);

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

So what I would like to do is something like this:

selectEmployee(d) {

          this.getData(d.employeeId).then(() => {
             // check data here and then execute the fetch
             this.$store.dispatch('fetchHistory');
          });

        },

But I get error in the then secuence, seems is not allowed there.

Any clue?

1
  • 3
    you aren't returning anything from getData Commented Jul 24, 2018 at 19:23

2 Answers 2

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...
           }
         });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Amazing!! Thanks alot
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');
},

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.