The correct approach to do that is to use vuex actions.
The nature of an action is to be asynchronous, so when you execute an action you can set an await for the action occur.
As stated by the documentation
Actions are similar to mutations, the differences being that:
- Instead of mutating the state, actions commit mutations.
- Actions can contain arbitrary asynchronous operations.
So you can await to that operation being performed before pass to the next operation.
As an example:
const store = new Vuex.Store({
state: {
token: null
},
mutations: {
SET_TOKEN (state, payload) {
state.token = payload
}
},
actions: {
SET_TOKEN ({ context }, token) {
context.commit('SET_TOKEN', token)
}
}
})
After that in your methods/hooks is just call an action via dispatch method.
methods: {
async refreshStoreValue() {
await this.$store.dispatch('SET_TOKEN', token) //this will call an action declared in your store.
}
}
You can use that statement inside a method declaration that is async, or inside your life cycle hooks (created, mounted, etc).