I'm new to Javascript and NodeJS. Im trying to read multiple CSV files before doing some processing on them. My current issue is when I run the code it tries to execute the processing before the reading of the file is complete. I would like to load both the CSVs before I start doing any processing on them.
Could some explain why this happens and how I can solve the problem in Javascript/NodeJS.
function readCSV(path){
var events = []
fs.createReadStream(path).pipe(csv()).on('data', (row) => {
events.push(row);
}).on('end', () => {
console.log('CSV file successfully processed. Length: ' + events.length);
});
return events
}
function app(){
var file_list = listFiles(folder_path);
for (let i = 0; i < file_list.length; i++) {
const file = file_list[i];
var events = readCSV(file)
}
processCSV(events) // Some processing
}
app();
Any help would be great and any explanation on how I can control when code is executed would be much appreciated.