title: Read lines synchronously from file in Node.js
body: <p>I need to parse a file line by line in the following format with Node.js:</p>
  
  <pre><code>13
  13
  0 5
  4 3
  0 1
  9 12
  6 4
  5 4
  0 2
  11 12
  9 10
  0 6
  7 8
  9 11
  5 3
  </code></pre>
  
  <p>It represents a graph. The first two lines are the number of edges and vertexes, followed by the edges.</p>
  
  <p>I can accomplish the task with something like:</p>
  
  <pre><code>var fs = require('fs');
  var readline = require('readline');
  var read_stream = fs.createReadStream(filename);
  var rl = readline.createInterface({
      input: read_stream
  });
  var c = 0;
  var vertexes_number;
  var edges_number;
  var edges = [];
  rl.on('line', function(line){
      if (c==0) {
          vertexes_number = parseInt(line);
      } else if (c==1) {
          edges_number = parseInt(line);
      } else {
          edges.push(line.split(' '));
      }
      c++;
  })
  .on('end', function(){
      rl.close();
  })
  </code></pre>
  
  <p>I understand this kind of things might not be what Node.js was thought for, but the cascaded <code>if</code> in the <code>line</code> callback does not really look elegant / readable to me. </p>
  
  <p>Is there a way to read synchronously lines from a stream like in every other programming language?</p>
  
  <p>I'm open to use plugins if there is not a built-in solution.</p>
  
  <p>[EDIT]</p>
  
  <p>Sorry, I should have made clearer that <strong>I would like to avoid loading the whole file in memory beforehand</strong></p>