0

Is there ever a case where both then() and catch() will fire?

read(start).then(() => {
  console.log('DONE:', h.addCommas(data));
}).catch((err)=>{
  console.log('ERROR:');
  console.log(err);
})
Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
  • 1
    what if the _then_ code raises an exception? – dandavis May 21 '19 at 19:10
  • ^ that would be the reason. (Or if it returns a rejected promise.) – Ry- May 21 '19 at 19:11
  • 2
    you can use `.finally()` if you want to react no matter what was called – Thomas May 21 '19 at 19:11
  • The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise. so whenever code written inside the promise raise exception then catch() method will fire so in this case both method will call – Sunny Goel May 21 '19 at 19:13
  • Possible duplicate of [Why do both Promise's then & catch callbacks get called?](https://stackoverflow.com/questions/40069638/why-do-both-promises-then-catch-callbacks-get-called) – Jacob Thomas May 21 '19 at 19:22

1 Answers1

2

Yes, both can fire.

Case 1: doSomething().then(successHandler).catch(errorHandler) - if doSomething is successful and successHandler throws, then errorHandler will fire (otherwise it won't).

Case 2: doSomething().catch(errorHandler).then(successHandler) - if doSomething throws and errorHandler doesn't throw, then successHandler will fire (otherwise it won't).

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44