Catch errors mid-chain?
See original GitHub issuetl;dr
Is catching errors in the middle of a promise chain considered bad-practice? Or is there a good reason for it?
Explanation
so I just tried the following chain
doSomething()
.then(function() {
throw new Error('contrived');
})
.catch(function(err) {
console.log('caught');
})
.then(function() {
console.log('got here');
});
And was a little confused that ‘got here’ gets printed. I guess the documentation makes this clear:
Any exception happening in a .then-chain will propagate to nearest .catch handler.
And the fact that catch is an alias for then(null, handler) makes a little more sense of the above behavior. However it still seems far from the conventional way seen in other OO languages that I’ve used so I was wondering whether this was on purpose or just a side-effect that should be avoided from an idiomatic perspective.
Issue Analytics
- State:
- Created 9 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Where should I .catch promise errors between separate ...
If all you want is for the promise chain to abort when an error occurs, then you can just put one .catch() at...
Read more >Error handling, "try...catch" - The Modern JavaScript Tutorial
So, try...catch can only handle errors that occur in valid code. Such errors are called “runtime errors” or, sometimes, “exceptions”.
Read more >GRE Tunnel Recursive Routing Error - NetworkLessons.com
GRE Recursive Routing errors occur when the router tries to reach the destination of the tunnel through the tunnel itself. I'll show you...
Read more >API to tapping Error · Issue #916 · mxcl/PromiseKit - GitHub
Hello,. For tapping value we can use get , for tapping both value and error we can use tap . But there is...
Read more >System Error Messages Guide For Access and Edge Routers
Explanation An invalid handle was encountered in the Access IE library. ... Explanation A midchain adjacency failed to stack onto output ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Well let’s map it to synchronus code according to the rules I gave and run it:
Logs
If that were true, then the following chain wouldn’t print ‘got here’:
So what functionally happens (haven’t looked into the code, only observed test behavior) is an implicit try between each catch, which I believe allows for some confusing code. I just don’t expect to see catches and finally’s in the middle of a promise chain and I expect other people with more OO backgrounds to feel similarly (though maybe it’s just me).