2

So is it possible that we can go into the second error block? Given that we have some promise rejection or errors that happen in the logic section

somePromise
  .then(function(data) {
      //some logic
      return something;
    })
    .then((data) => {
      // more logic
    }, function(err) {
      // first error block
    })
    .catch(function(err) {
      // second error block
    });
nanomosfet
  • 132
  • 11

3 Answers3

2

Yes, it's possible - if the second function passed to the .then throws an error (or returns a rejected Promise), the error will be passed down to the next .catch:

Promise.resolve()
  .then(function(data) {
      //some logic
      throw new Error();
    })
    .then((data) => {
      // more logic
    }, function(err) {
      // first error block
      console.log('Handling first error');
      return err.somePropertyThatDoesNotExist.text;
    })
    .catch(function(err) {
      // second error block
      console.log('Handling second error')
    });

As comment notes, the catch will also run if the first function passed to the second .then throws an error:

Promise.resolve()
  .then(function(data) {
      //some logic
      return 'Foo';
    })
    .then((data) => {
      // more logic
      throw new Error();
    }, function(err) {
      // first error block
      console.log('Handling error in then');
    })
    .catch(function(err) {
      // second error block
      console.log('Handling error in catch')
    });
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • IMHO I should also be called if the first function (`// more logic`) throws an error. The second function (`// first error block`) will only catch the previous errors, and not the ones in (`// more logic`) – t.niese Sep 06 '18 at 04:49
  • Follow up question: is there any reason to writing it like that? Could we just make it work with one error handling function? – nanomosfet Sep 06 '18 at 21:09
  • 1
    @nanomosfet No, not really - see [When is .then(success, fail) considered an antipattern for promises?](https://stackoverflow.com/questions/24662289/when-is-thensuccess-fail-considered-an-antipattern-for-promises). You nearly always want to use `.catch` alone instead (and ensure that the `catch` itself doesn't throw anything). – CertainPerformance Sep 06 '18 at 21:48
0

Yes the callback of .catch will be called if an error occurred in either the // more logic or the // first error block block.

// first error block won't be called for errors happening in // more logic

t.niese
  • 39,256
  • 9
  • 74
  • 101
0

Yes it will catch the error in first block here is the below example for the same.

var promise1 = new Promise(function(resolve, reject) {
  throw 'Ohh Nooo!';
});

promise1.then((data) => {
  // more logic
}, function(err) {
  // first error block
  console.log("Catch Error Block 1");
}).catch(function(error) {
  console.log(error);
});
// expected output: Uh-oh!
t.niese
  • 39,256
  • 9
  • 74
  • 101
vijay sahu
  • 765
  • 1
  • 7
  • 29
  • Why do you write `// expected output: Uh-oh!`, if you show an example that outputs only `Catch Error Block 1`. And how does that answer the question. The OP is asking under which circumstance the `.catch(function(error) {` would be called. – t.niese Sep 06 '18 at 07:13