Reputation: 1563
As I can see, it is common to see this line:
if (err) return done(err);
But as I understand it right, I can do this:
if (err) throw err;
Which looks pretty and works good. So what's a difference?
And yea, I know about promises and I like 'em, but still, I want to know answer for this question.
Upvotes: 2
Views: 74
Reputation: 1214
Since most errors are asynchronous, you probably want to use a callback to propagate the error back to the error handler.
Try/catch blocks are seldomly used, and only for the few synchronous functions which might fail, like JSON.parse()
.
Upvotes: 1
Reputation: 13570
You can do
if (err) throw err;
if the error is fatal and you want your program to crash, because there is no way to catch such an error. If you write a library or a server application you should pass error to callback.
Upvotes: 2