Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Then Function Running before Store Action in Vue

So in my brands.js store I have this action which calls the API:

  getBrandMerchants({ commit }, id) {
    commit('setLoading', true);
    BrandService.getBrandMerchants(id)
      .then((response) => {
        commit('setBrand', formatter.deserialize(response.data.result.brand));
        commit('setMerchants', formatter.deserialize(response.data.result.merchants));
        commit('setLoading', false);
        console.log('First');
      })
      .catch((error) => {
        if (error.response.status === 401) {
          dispatch('alert/error', error.response, { root: true });
        } else {
          Toast.open({
            type: 'is-danger', message: error.response.data.meta.message,
          });
        }
      });
  },

In my components I have the following snippets:

...mapState('brands', ['merchants']),
...mapActions('brands', ['getBrandMerchants']),

When I try to run this method in my component:

    addMerchantsToBrandGroup(index) {
      const { id } = this.rows[index].brand;
      if (id === null) { return; }

      this.getBrandMerchants(id)
        .then(() => {
          console.log('Second');
          this.rows[index].merchants = this.merchants
        });
    },

console results to

Second
First

How can I make it so console returns First then Second?

First
Second
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As mentioned by @deceze:

Adding async and await to:

  async getBrandMerchants({ commit }, id) {
    commit('setLoading', true);
    await BrandService.getBrandMerchants(id)
      .then((response) => {
        commit('setBrand', formatter.deserialize(response.data.result.brand));
        commit('setMerchants', formatter.deserialize(response.data.result.merchants));
        commit('setLoading', false);
        console.log('First');
      })
      .catch((error) => {
        if (error.response.status === 401) {
          dispatch('alert/error', error.response, { root: true });
        } else {
          Toast.open({
            type: 'is-danger', message: error.response.data.meta.message,
          });
        }
      });
  },

and

async addMerchantsToBrandGroup(index) {
      const { id } = this.rows[index].brand;
      if (id === null) { return; }

      await this.getBrandMerchants(id)
      console.log('Second');
      this.rows[index].merchants = this.merchants
    },

Solved the problem. Thanks!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!