[Vuejs]-How do I write a perfect vue.js action to fetch data from server with proper error handling?

0👍

const error = () => alert('Could not load data, Please try again later');

Vue.http.get('/environments', { params })
  .then(response => {
    const data = response.json();
    if (data) {
      commit('mutateUpdateEnvironmentData', data);
    } else {
      error();
    }
  }, error);

or

Vue.http.get('/environments', { params })
  .then(response => response.json(), () => null)
  .then(data => {
    if (data) {
      commit('mutateUpdateEnvironmentData', data);
    } else {
      alert('Could not load data, Please try again later');
    }
  });

Leave a comment