title: Node read line by line, process and store
body: <p>I have the following code in Node.js which reads from a file, line by line. I want to do stuff to each line and store it in an array. The array would then be used in other functions in the same file. The problem I'm running into is the async nature of reading the stream which results in an empty array. The solutions I've come across all seem to rely on modules.</p>
  
  <pre><code>function processLine(file) {
    const fs = require('fs');
    const readline = require('readline');
    const input = fs.createReadStream(file);
    const rl = readline.createInterface(input);
    const arr = []
  
    rl.on('line', (line) =&gt; {
      // do stuff to data and store in array
    })
    // return array;
  }
  </code></pre>
  
  <p>I am aware of being able to store the chunks and operate on the whole file with <code>input.on('end', cb)...</code> However, I feel like this would put too much functionality within the cb. Plus I still can't use its return value since its async. I guess my question is, is there a way to store data being read and use it within the file?</p>