DEV Community

Discussion on: Structuring store with the right feet using vue/vuex

Collapse
 
tobymosque profile image
Tobias Mesquita

great article, but i think actions who does async tasks would return a promise.

GET_DATA({commit}){
  return A.get(API_URL_ENDPOINT).then((res) =>{
    commit('SET_DATA' , res.data);
    setTimeout(() =>{
      commit('IS_LOADING' , false);
    } , 1000)
  }).catch((err) =>{
    console.error(err)
  });
}

anyway, i think IS_LOADING would be set to false, even if the http request throws a error.

async GET_DATA({commit}){
  try {
    const { data } = await A.get(API_URL_ENDPOINT)
    commit('SET_DATA' , res.data)
  } catch () {
    console.error(err)
  } finally () {
    setTimeout(() =>commit('IS_LOADING' , false), 1000)
  }
}