I have an array that is bound to a vuex data store and exposed as a computed property through the mapGetters
helper. This array is called items
, and I call a REST API to update it in the created()
hook of the component. The vuex action I have for this returns a promise in which the API is accessed and the mutator called (which updates the items
array) before resolving. My understanding of promises tells me that my then
calls should safely occur after the asynchronous action completes, yet if I try to access items
from the promise resolution, it is empty despite the array certainly being populated by the API call. Why isn't this working how I expect?
The code is all over but here are the relevant parts
Component:
computed: {
...mapGetters({
items: 'allHistoryItems'
}),
// ...
created () {
this.$store.dispatch('fetchList').then(console.log(this.items))
}
Action:
fetchList: ({ commit }) => {
return new Promise((resolve, reject) => {
fetchList().then(response => {
commit(types.FETCH_LIST, response.data)
resolve(response)
})
})
}
I can access the API response from the component, but as mentioned, items
is empty. Does the reactivity not kick in until after the promise resolves?