Vuex: Does dispatch return a promise so that you can chain them?

Viewed 237

I am not coding my app yet so I can't test it out but if I wanted to chain dispatches, is that possible to make my dispatches run synchronously?

example:

this.$store.dispatch('doSomething', {
    'url': '/admin/do-something',
    'user_id': userId,
})
.then(response => {
    this.$store.dispatch('doSomething2', {
       'url': '/admin/do-something-2',
       'user_id': userId,
    })
})
1 Answers

Answer

According to the vuex documentation:

dispatch

dispatch(type: string, payload?: any, options?: Object): Promise<any>
dispatch(action: Object, options?: Object): Promise<any>

Meaning, dispatch will always return a Promise so, theoretically, yes you can chain the dispatches to ensure a specific order

Extra

Going further, if these actions always need to happen in this order, consider making a single action that will dispatch each of these individually for you:

someVuexModule.js

...
export const actions = {
  async doSomethingMaster({ dispatch }) {
    await dispatch('doSomething');
    await dispatch('doSomething2');
    await dispatch('doSomething3');
    
  },
  doSomething() {},
  doSomething2() {},
  doSomething3() {},
}

...
Related