I'm trying to read in from a text file and store them in a array of my Lease class. If I console.log() from inside the my final loop it will print the loop in its current state for each iterration. But if I move the console.log outside the loop it prints an empty array.
working as expected
const readline = require('readline'),
fs = require('fs');
function Lease(renter, unit) {
this.unit = unit;
this.renter = renter;
}
var list = [];
var rl = readline.createInterface({
input: fs.createReadStream('input.txt'),
output: process.stdout,
terminal: false
});
rl.on('line', function(line) {
var values = line.split(' - ');
list.push(new Lease(values[0], values[1]));
console.log(list);
});
printing empty array
const readline = require('readline'),
fs = require('fs');
function Lease(renter, unit) {
this.unit = unit;
this.renter = renter;
}
var list = [];
var rl = readline.createInterface({
input: fs.createReadStream('input.txt'),
output: process.stdout,
terminal: false
});
rl.on('line', function(line) {
var values = line.split(' - ');
list.push(new Lease(values[0], values[1]));
});
console.log(list);