Single commit/dispatch in @Action called only.
See original GitHub issueIn the official Vuex-documentation it is stated, that multiple commits in a single action are possible. For example here:
actions: {
checkout ({ commit, state }, products) {
// save the items currently in the cart
const savedCartItems = [...state.cart.added]
// send out checkout request, and optimistically
// clear the cart
commit(types.CHECKOUT_REQUEST)
// the shop API accepts a success callback and a failure callback
shop.buyProducts(
products,
// handle success
() => commit(types.CHECKOUT_SUCCESS),
// handle failure
() => commit(types.CHECKOUT_FAILURE, savedCartItems)
)
}
}
Now when I tried to get a similar behavior with this neat module, only the first commit/dispatch gets called and the action ends afterwards. Here is an example:
@Mutation
public [PURGE_AUTH]() {
this.token = '';
this.user = {id: '', username: ''};
localStorage.removeItem('token');
delete axios.defaults.headers.common['X-AUTH-TOKEN'];
}
@Action
public async [LOGOUT]() {
await doLogout();
console.log('COMMITTING');
this.context.commit(PURGE_AUTH);
console.log('DISPATCHING');
this.context.dispatch(RESET_ALL);
console.log('AFTER DISPATCH');
return;
}
Only ‘COMMITTING’ will be printed to the console and the the other dispatches/commits do not get called. I have also tried to use the await
keyword for the commits but that does not do anything.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Vue - call async action only after first one has finished
When you dispatch an action, it doesn't have a then callback by default. That's only the case if the action returns a Promise...
Read more >Actions | Vuex
Actions are similar to mutations, the differences being that: Instead of mutating the state, actions commit mutations. Actions can contain ...
Read more >Stop using actions in Vuex | JavaScript in Plain English
You don't call the “action” directly, but instead, call a dispatch method and give the name of your “action method”. You rely on...
Read more >How To Manage State in a Vue.js Application with Vuex
This way you can have multiple mutations inside of an action. The cardinal rule of Vuex is that mutations have one job and...
Read more >What are Vuex Actions? - Mastering JS
To "call" an action, you should use the Store#dispatch() function. ... any function commit mutations, so you could just as easily do this:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found a work-around for the issue, I don’t know if it is considered hacky or not but that’s how I solved the problem:
I know this code is a little different than above but I think one might get my point. I realized after doing a few tests, that the reference to context was lost as soon as something like
await function()
was called, meaning that I could not commit, nor dispatch viathis.context
. By setting a reference to the context I managed to do those commits and dispatches.I would also recommend to enable
rawError
to be able to catch errors that could occur in commits/dispatches.I will look whether I find the time to make a PR, but maybe for now this is enough.
Yes the issue has been solved for me.