I found the following code in a tutorial:
promise.then(function(result){
//some code
}).catch(function(error) {
throw(error);
});
I\'m
There is no point to a naked catch and throw as you show. It does not do anything useful except add code and slow execution. So, if you're going to .catch()
and rethrow, there should be something you want to do in the .catch()
, otherwise you should just remove the .catch()
entirely.
The usual point for that general structure is when you want to execute something in the .catch()
such as log the error or clean up some state (like close files), but you want the promise chain to continue as rejected.
promise.then(function(result){
//some code
}).catch(function(error) {
// log and rethrow
console.log(error);
throw error;
});
In a tutorial, it may be there just to show people where they can catch errors or to teach the concept of handling the error, then rethrowing it.
Some of the useful reasons for catching and rethrowing are as follows:
But, a plain catch and rethrow of the same error with no other code in the catch handler doesn't do anything useful for normal running of the code.