score:2

Accepted answer

i think that you can handle if the response is success but it doesn't has data on i, or if the request just got an error the catch statement can handle it :

getdata()
  .then(response =>{
    if(!response.data) { 
     dispatch({ type: 'some_action_for_the_error', payload: response.error });
    } 
    else {
     transformdata(response)
    }
  ).catch(( error ) =>{
    console.error(error);
    dispatch({ type: 'some_action_for_the_error', payload: error });
  }

score:0

the .then() function can take two arguments. the first is a success callback, which you are using. the second is a failure callback, which is what you want.

getdata().then(
  success => transformdata(success),
  failure => dosomething(failure)
);

score:0

first you have to understand that how promise works. then() method has two functions. first is success and second is failure. failure can be written with .catch in es6. now first understand that catch only gets called for network failure, 500 error, 404 not found error. in that case there is no data to process.

but what you would want is 200 ok but if data is missing. you have put code in success itself :

getdata().then(response => {
  const res = transformdata(response);
  if(!res.data) {
     // do your stuff
  }
   return res; // else normal behaviour
});

Related Query

More Query from same tag