title: Node.js return, callback and source code execution flow - Runtime
body: <p>I'm pretty confused by the way to work on node.js, I'm talking about callbacks, returns and how source code is executed.</p>
  
  <p>I'm using sails.js but I don't think it's linked, it's more the way to JS works, I think.</p>
  
  <p>Source code:</p>
  
  <pre><code>module.exports = function (req, res, callback) {
      if (req.session.authenticated) {
          // Accept user authentication.
          return callback();
      } else {
          // Not authenticated. Try auto authentication.
          if(validator.check([validator.rulesHardwareId(req.param('hardwareId'))])){
              Device.findOne({hardwareId: req.param('hardwareId')}, function(err, device){
                  if(err){
                      return res.json({message: "You are not permitted to perform this action. You have to connect to the platform before. [Wrong hardwareId or DB error]", data: {err: err}, status: false}, 403);
                  }
  
                  if(device){
                      // Connect the device.
                      req.session.authenticated = true;
                      req.session.from = 'device';
  
                      // Search for the device's owner.
                      User.findOne({id: device.userId}, function(err, user){
                          if(err){
                              return res.json({message: "DB error.", data: {err: err}, status: false}, 403);
                          }
  
                          if(user){
                              // Add data about the user.
                              req.session.user = user;
                              return callback();
                          }else{
                              return res.json({message: "Device found but device's owner doesn't found.", data: {err: err}, status: false}, 403);
                          }
                      });
                  }else{
                      return res.json({message: "You are not permitted to perform this action. You have to connect to the platform before. [Wrong hardwareId]", data: {err: err}, status: false}, 403);
                  }
              });
          }
          return res.json({message: "You are not permitted to perform this action. You have to connect to the platform before. [Give hardwareId ?]", data: {}, status: false}, 403);
  
      }
  };
  </code></pre>
  
  <p>The code is not so important, the thing is that I received this message: 
  "You are not permitted to perform this action. You have to connect to the platform before. [Give hardwareId ?]"</p>
  
  <p>But the action is CREATED. Okay, so I call the callback() and I return it, but the source code continue? And execute the last line? Why? I don't understand that.
  If I put the last line in a ELSE, I receive the message "The action is created".</p>
  
  <p>If someone could explain me.. I thought that add the return keyword was useful for prevent this, but it looks like I'm wrong.</p>
  
  <p>Thank you.</p>