Kris D. J.
Kris D. J.

Reputation: 72

Returning axios promises from vuex action

I can't figure out, if it's a bad approach to have vuex actions returning axios promises like the example below. The reason why I do it, is because I need some values from the vuex state in the request. Can anyone tell me, if this is bad to do or totally fine? :)

/* Vuex action */
fetchSomething({ state }, id) {
  return getSomething(
    id,
    state.basket.shipping,
    state.basket.billing);
},

/* Method in vue component */
someMethod() {
  this.$store.dispatch('fetchSomething')
    .then(res => console.log(res));
}

Upvotes: 1

Views: 383

Answers (1)

Lassi Uosukainen
Lassi Uosukainen

Reputation: 1688

I don't see the point of doing this. The point of a Vuex action is to do the asynchronous job of retrieving the data and then to mutate the Vuex store.

Once the store has been mutated, you can access it from your Vue component. As Vuex is part of the Vue ecosystem it follows the same reactivity principles as Vue. If you need to execute something once you have retrieved the data you can set a watcher on the variable you are retrieving or more commonly just use it as a computed property.

Upvotes: 2

Related Questions