Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda