0

I have some code that looks something like this:

promiseObject.then(
  function(data) {console.log('Success')},
  function(err) {console.log('Error')}
).catch(
  function(err) {
    console.log('Error')
  })

If the promiseObject does not resolve the intention is to log 'Error'.

Am I right in thinking that the catch method is an alternative to having a reject handler? So that in this example the first log of 'Error' is redundant?

  • No, it's not redundant, it's just *different*. I hope the duplicate gives you all information that you need. – Bergi Apr 27 '16 at 15:15
  • Thanks Bergi. That answer is interesting. To help me understand this I created a JSFiddle https://jsfiddle.net/justinwyllie/ewz509s4/ . It seems that having a reject handler causes the catch() method not to be called. Though - according to this documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch the catch method will catch an error in the success handler. Based on that am I right in thinking that the purpose of the catch method is to catch errors in the success handler. So it is not an alternative to the reject handler. It is different! –  Apr 27 '16 at 16:25
  • Yes, if you chain multiple rejection handlers after each other (regardless whether via `then` or `catch`), only the first will get called unless another exception is thrown. See [here](http://stackoverflow.com/q/16371129/1048572) for details – Bergi Apr 27 '16 at 16:46
  • Thanks Bergi. The specific point about only the first being called regardless of whether via then or catch - was very helpful. –  Apr 29 '16 at 13:36

0 Answers0