title: Node.js sync vs. async
body: <p>I'm currently learning node.js and I see 2 examples for sync and async program (same one).</p>
  <p>I do understand the concept of a callback, but i'm trying to understand the benefit for the second (async) example, as it seems that the two of them are doing the exact same thing even though this difference...</p>
  <p>Can you please detail the reason why would the second example be better?
  I'll be happy to get an ever wider explanation that would help me understand the concept..</p>
  <p>Thank you!!</p>
  <p>1st example:</p>
  <pre><code>var fs = require('fs');
  
  function calculateByteSize() {
      var totalBytes = 0,
          i,
          filenames,
          stats;
      filenames = fs.readdirSync(&quot;.&quot;);
      for (i = 0; i &lt; filenames.length; i ++) {
          stats = fs.statSync(&quot;./&quot; + filenames[i]);
          totalBytes += stats.size;
      }
      console.log(totalBytes);
  }
  
  calculateByteSize();
  </code></pre>
  <p>2nd example:</p>
  <pre><code>var fs = require('fs');
  
  var count = 0,
      totalBytes = 0;
  
  function calculateByteSize() {
      fs.readdir(&quot;.&quot;, function (err, filenames) {
          var i;
          count = filenames.length;
  
          for (i = 0; i &lt; filenames.length; i++) {
              fs.stat(&quot;./&quot; + filenames[i], function (err, stats) {
                  totalBytes += stats.size;
                  count--;
                  if (count === 0) {
                      console.log(totalBytes);
                  }
              });
          }
      });
  }
  
  calculateByteSize();
  </code></pre>