Search code examples
node.jscallbackflow-control

Node callbacks; else blocks vs return


I often see node.js programmers, when calling functions that expect callbacks, do this:

callSomeThing(arg1, arg2, function (err, data) {
   if(err) {
       // Handle the error case
   } else {
       // Proceed normally
   }
});

Is there any technical reason why they use an else block instead of returning in the error case or is it just a code style thing?

callSomeThing(arg1, arg2, function (err, data) {
   if(err) {
       // Handle the error case
       return;
   } 
   // proceed normally
});

Solution

  • Its just a matter of coding style. But I would recommend the second approach, because its much clearer and reduceses the overall complexity.