0

I have a call in a Promise chain that might fail, and in certain conditions I want to ignore that error. Is it possible to tell the promise that it should continue as a success?

Like this:

function do() {
  return possiblyFailingCall.then(function(response) {
    // handle successful response
    return response.data;
  }, function (error) {
    if (error && error.statusCode === 404) {
      // ah, this is not really an error; I wanto to return an empty object now
      return {}; // <- this is where I want to return to the success-route
    } else {
      // this is actually an error that I want to pass on in the promise chain
    }
  });
}

do().then(function(data) {
  // in the case of the 404 (not found), I want to get an empty object
}, function(error) {
  // I do not want to see the 404 error here, but I want to see all other errors
});
daniel kullmann
  • 13,653
  • 8
  • 51
  • 67
  • 2
    Does this answer your question? [Return success when handling error in a promise](https://stackoverflow.com/questions/52831101/return-success-when-handling-error-in-a-promise) – KyleRifqi Jan 28 '22 at 12:37
  • That's how you do it - you return a regular value from the error/catch handler. Does it not work in your code? – VLAZ Jan 28 '22 at 12:38
  • You are 95% done. `return {}` puts empty object on the success path. In the `else` clause, simply throw an error, eg `throw error || new Error('something bad happened that wasn't a 404');` – Roamer-1888 Jan 28 '22 at 13:40

0 Answers0