Search code examples
javascriptvue.jsvuexes6-promisevuex-modules

Why does Promises.all doesn't work in my vuex store


I'm facing a weird issue right now. I have a vuex store in my vue project which is seperated in different modules. I want to use Promise.all() to execute two independent async vuex action at once to enjoy the advantage of the first fail behavior.

store/modules/categories:

    async CATEGORIES({ rootState }) {
        const response = await axios.post('link_to_api', {
            // some arguments for the api
            arg: rootState.args
        })

        return response
    }

store/modules/transportation:

    async TRANSPORTATION({ rootState }) {
         const response = await axios.post('link_to_api', {
            // some arguments for the api
            arg: rootState.args
        })

        return response
    }

I know want to call those async functions in Promise.all:

store/modules/categories:

    async PUT_CATEGORIES({ commit, dispatch, rootState }) {
      try {
         const [resCategories, resTransportation] = await Promise.all([
            dispatch('CATEGORIES').catch(err => { console.log('Fehler bei Kabinenabfrage!'); throw {error: err, origin: 'kabine'}; }),
            dispatch('transportation/TRANSPORTATION', {root:true}).catch(err => { console.log('Fehler bei Flugabfrage!'); throw {error: err, origin: 'flug'}; })
        ]) 
         //do something after both promises resolved

      } catch(error) {
            // do something if one promise rejected
            commit('errorlog/ERROR', 4, {root:true})
            dispatch("errorlog/LOG_ERROR", {'origin': '2', 'error_code': '113', 'message': error.toString()}, {root:true})
            router.push({path: '/Error'})
        }  

I get the following error:

error message

This is weird because I used {root:true} and the prefix transport in dispatch to access the action of the transport module in store. This works great for the LOG_ERROR action in the errorlog module I use in the catch block.

If I copy the TRANSPORTATION action in the categories module it works great...

Has anybody faced this issue before and has an advice??

Thanks in advance!


Solution

  • In your case, {root:true} is passed as second argument although it should be passed as third.

    - dispatch('transportation/TRANSPORTATION', {root:true})
    + dispatch('transportation/TRANSPORTATION', null, {root:true})
    

    According to vuex's doc

    To dispatch actions or commit mutations in the global namespace, pass { root: true } as the 3rd argument to dispatch and commit.

    They also provide a sample code (which is further simplified here)

    modules: {
      foo: {
        namespaced: true,
        actions: {
          // dispatch and commit are also localized for this module
          // they will accept `root` option for the root dispatch/commit
          someAction ({ dispatch, commit, getters, rootGetters }) {
            dispatch('someOtherAction') // -> 'foo/someOtherAction'
            dispatch('someOtherAction', null, { root: true }) // -> 'someOtherAction'