I am trying to read file from a .txt file in nodejs, when i get access to each line, I push it to an array but in the end, the array is empty.
var array=[];
let lineReader = require('readline').createInterface({
input: require('fs').createReadStream('file.txt')
});
lineReader.on('line', function (line) {
console.log(line);
array.push(line);
});
console.log(array);
.Txt File Content
THIS IS LINE#1
THIS IS LINE#2
THIS IS LINE#3
Output
[]
THIS IS LINE#1
THIS IS LINE#2
THIS IS LINE#3
lineReader.on('line', function (line) {...})is not synchronous ( mean, the function you pass to it is not called synchronously).on(...)) is not synchronous (as you can see in the output)