title: Simple nodejs callback example with fs.readFile
body: <p>I'm trying to learn async programming and was struggling with lesson 4 of nodeschool.io with the implementation of an async io with callbacks.</p>
  
  <p>Basically, I'm trying to use fs.readFile to count the number of newlines within a file using a callback.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>var fs = require('fs');
  var pathToFile = process.argv[2];
  
  function counter(callback) {
      var buffer = fs.readFile(pathToFile, function (err, data) {
      var bufferString = buffer.toString();
      var bufferStringSplit = bufferString.split('\n');
    });
    callback();
  }
  
  function logMyNumber() {
    console.log(bufferStringSplit.length-1);
  }
  
  counter(logMyNumber);
  </code></pre>
  
  <p>I understand that callbacks are executed once the line of code is finished executing, so shouldn't the </p>
  
  <pre><code>var bufferString = buffer.toString();
  var bufferStringSplit = bufferString.split('\n');
  </code></pre>
  
  <p>be called after fs.readFile() finishes reading the file from disk?</p>
  
  <p>Then finally the callback() calls logMyNumber, which should just output the number of lines the file has.</p>