title: How Nodejs knows if sync or async
body: <p>I understand what a callback is and what asynchronous means, what I don't get is how to run asynchronous functions in node.</p>
  
  <p>For example, how is this</p>
  
  <pre><code>var action = (function(data,callback) {
      result = data+1;
      callback(result);
  });
  
  http.createServer(function (req, res) {
      action(5, function(r){
          res.end(r.toString());
      });
  }).listen(80);
  </code></pre>
  
  <p>different from this</p>
  
  <pre><code>var action = (function(data) {
      result = data+1;
      return result;
  });
  
  http.createServer(function (req, res) {
      var r = action(5);
      res.end(r.toString());
  }).listen(80);
  </code></pre>
  
  <p>?</p>
  
  <p>I guess in the first example I'm doing it asynchronously, yet I don't know how Node knows when to do it sync or async... is it a matter of the return? or the fact that in the sync mode we're doing <code>var x = func(data);</code>?</p>
  
  <p>And also: when to use sync or async? Because obviously you don't want to use it when adding +1... is it OK to use async just when performing IO tasks, such as reading from DB? </p>
  
  <p>For example, I'm using the library crypto to encrypt a short string (50 chars at most), is this case a good example where I should already be using async?</p>