How does the β€œcatch” work in the native Promise chain?

Try this code snippet on the Chrome or Firefox console tab

var p = new Promise(function(resolve, reject) { setTimeout(function() { reject(10); }, 1000) }) p.then(function(res) { console.log(1, 'succ', res) }) .catch(function(res) { console.log(1, 'err', res) }) .then(function(res) { console.log(2, 'succ', res) }) .catch(function(res) { console.log(2, 'err', res) }) 

Result will be

 1 "err" 10 2 "res" undefined 

I have tried many other examples, but it seems that the first then() returns a promise that always resolves and never rejects. I tried this on Chrome 46.0.2490.86 and Firefox 42.0. Why is this happening? I thought then() and catch() could be integer multiple times?

+6
source share
2 answers

As in synchronous code:

 try { throw new Error(); } catch(e) { console.log("Caught"); } console.log("This still runs"); 

The code that runs after the exception is processed will be launched - this is because exceptions are a mechanism for error recovery. By adding this catch, you indicated that the error was handled. In the synchronous case, we deal with this by reorganizing:

 try { throw new Error(); } catch(e) { console.log("Caught"); throw e; } console.log("This will not run, still in error"); 

Promises work similarly:

  Promise.reject(Error()).catch(e => { console.log("This runs"); throw e; }).catch(e => { console.log("This runs too"); throw e; }); 

As a hint, never fail with Error , as you lose a lot of useful things, such as meaningful stack traces.

+7
source

Why is this happening? I thought then () and catch () could be integer multiple times?

@Benjamin is right, +1, but to put it another way, these are the rules:

  • If you added then several times, you will be whole methods that should be called sequentially until an exception is thrown. Exceptions in the then chain should be handled by catch declared after then . If after then there is no catch , this error will fire: Uncaught (in promise) Error(…) .
  • If you add catch several times, you bind methods that should be called when something goes wrong (in then functions before). However, the second catch in the chain will only be called if the first one throws an exception throw, etc.
  • When catch fires, the chain resumes on the next then declared after catch .
+2
source

All Articles