I'm trying to read in a csv file line by line, I've got the line by line reader working but now I need to put it in a callback to ensure its finished reading before I move on to process the data. However, I think I've misunderstood how to use a callback to do so because the array I'm trying to fill is still empty.
var data = [],
power = null,
wattH = null,
watts = null,
maxTemp = null,
averageTemp = null,
maxCurrent = null,
maxPower = null,
totalTime = null;
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(__dirname + '/logs/MOCK_DATA.csv')
});
function readlines() {
getLines(function(lines) {
data = lines;
});
}
function getLines(callback) {
var out = [];
lineReader.on('line', function(line) {
out.push(line);
});
callback(out);
}
readlines();
console.log(data);
Any help would be greatly appreciated :)
EDIT: The following worked
function readlines(callback) {
var out = [];
lineReader.on('line',callback, function(line) {
out.push(line);
});
callback(out);
}
readlines(function(result){
console.log(result);
});