title: Accessing data parsed by readline & fs in node.js outside of the callback function
body: <p>This question is different from the one linked as already having an answer. It is specifically this piece of code adapted from node.js documentation regarding the use of fs and readfile and looking for an end of file flag, which I've learned is readfile.close method.  Thanks for the answer. </p>
  
  <p>I wrote a small utility locally to try to convert a text file of <code>key:value</code> pairs with a blank line separating programs into a JSON file to use in a React project. </p>
  
  <p>I got the foundation of the readline function directly from node.js documentation. I'm using node 6.9.0 on my mac</p>
  
  <p>here is the full script: </p>
  
  <pre><code>const readline = require('readline');
  const fs = require('fs');
  
  const rl = readline.createInterface({
    input: fs.createReadStream('usat-ncaa-programs.txt')
  });
  
  var newPairs=["test"];
  rl.on('line',
    function (line) {
    if (line===null){
      newPairs.push("}], [ {")
    } else if (line) {
      var keyValue = line.match(/^(.*):(.*)/)
      var newKeyValuePair =  "'" + keyValue[1].trim() + "':  '" + keyValue[2].trim() + "'"
      newPairs.push(newKeyValuePair)
      //console.log (newKeyValuePair)
    }
  
  })
  
  console.log(newPairs)
  </code></pre>
  
  <p>The input file looks like this (there are about 12 programs), i've only included 2 1/2 so you can see the format: </p>
  
  <pre><code>University: Arizona State University
  Division: I
  University Home Page: http://www.asu.edu/
  Recruiting Link: https://questionnaire.acsathletics.com/Questionnaire/Questionnaire.aspx?&amp;SPSID=1061112&amp;SPID=177408&amp;DB_LANG=C&amp;DB_OEM_ID=30300&amp;q=2015&amp;s=159130&amp;o=143
  Team Homepage: http://www.thesundevils.com/index.aspx?path=triathlon
  Head Coach: Cliff English
  w: 480.965.0546
  e: cliff.endlish@asu.edu
  bg-color: #990033
  color: #FFB310
  
  University: Belmont Abby College
  Division: II
  University Home Page: http://belmontabbeycollege.edu/
  Recruiting Link: https://coach.scoutforce.com/p/414f3219dd
  Team Homepage: http://abbeyathletics.com/sports/wtri/index
  Head Coach: Nick Radkewich
  w: 704.461.5010
  e: NicholasRadewich@bac.edu
  Twitter: https://twitter.com/AbbeyTri
  bg-color: #FFFDD0
  color: #DC143C
  
  University:Black Hills State University 
  Division: II
  University Home Page: http://www.bhsu.edu/
  ...
  </code></pre>
  
  <p>My problem is that while I can read the text file line by line and parse some information that looks like a JSON file, I am unable to access that data outside of the callback function. </p>
  
  <p>I don't know how to save this data into a new file, or even just output the object to my console for a cut &amp; paste and manually edit. </p>
  
  <p>In teh above script the logged output of the variable newPairs is ["test"] rather than the line by line parsing that I'm trying to acccomplish.</p>
  
  <p>If I place the console.log inside the callback, I get information logged with every iteration of the file read.  I only want to work with the data when the file is done. </p>
  
  <p>I did not find an <code>EOF</code> or similar flag in the node documentation for either <code>fs</code> or <code>readline</code>.</p>
  
  <p>Additionally, if there is an easier way to get the data I am inputting into JSON format, I'd love to hear. Thanks in advance. </p>