0

I use the following code to read the file line by line. But the content of the line is in a closure, what can I do to access the contents of the line outside?The module I am using is the internal module of node.js

  var fs=require("fs")
 function readFirstLine(){
    var firstfile = [];
    readline = require('readline');

    var rd = readline.createInterface({
        input: fs.createReadStream('somefile.txt'),
        output: process.stdout,
        terminal: false
    });

    rd.on('line', function(line) {
        console.log(line)  
    }
    );

}
    readFirstLine()
lisinan
  • 71
  • 7

1 Answers1

0

You can push each line to external array, but you won't know when the readline finish to read the file.

The easiest way, with async/await.

How do I return the response from an asynchronous call?

yeya
  • 1,968
  • 1
  • 21
  • 31