0

So I have this code:

aPromise
.then(function(result2){return aFailingPromise;}, function(error) {...})
.then(function(result1){return newPromise;}, function(error) {/*Catch failure, do backup things and go back to correct track*/})
.then(function(result3){return promise;}, function(error) {...});

When a failure occurs, every error method that I pass into the promise chain gets called and does not go back to the 'success' track. How can I do this? One of my promises has a possible backup plan, that I want to execute in the error method and perform the rest of the successes.

vrwim
  • 13,020
  • 13
  • 63
  • 118
  • whats in your error handler? – Johannes Merz Feb 28 '18 at 14:50
  • Currently `return response.error(error)` (this promise chain is inside of a Parse define), but I would like to go to the first function (indicating success) on the next item in the promise chain – vrwim Feb 28 '18 at 14:53
  • What makes you think that the error handlers "*do not go back to the 'success' track*"? That's *exactly* what they do. You might want to have a look at the [difference between `.then(…, …)` and `.then(…).catch(…)`](https://stackoverflow.com/q/24662289/1048572), though. – Bergi Feb 28 '18 at 15:24

1 Answers1

0

You return to success if you don't throw/reject in your error handling

Promise.reject(new Error('some error'))
  .then(() => console.log('never reached'))
  .catch(e => console.log(e.message)) // some error
  .then(() => console.log('good again as the error is handled above'))
  .catch(() => console.log('never reached'))
Johannes Merz
  • 3,252
  • 17
  • 33
  • So this will catch the error from the previous item in the promise chain? What do I put if I want to end the promise chain? – vrwim Feb 28 '18 at 14:54
  • 2
    you cant really, if you want conditional promise chains you have to nest them. As this it pretty broad i would suggest you to formulate a specific use case/flow. I can help you with that then. – Johannes Merz Feb 28 '18 at 15:14