I'm trying to figure out how to solve this problem:
I have a function that reads a csv, saves it to an array and then returns the array. My problem: It always returns an empty array, since the filestream hasn't finished, before I try to return the array.
const parser = require("csv-parser");
const fs = require("fs");
class CsvConverter {
constructor() {}
convert(filePath) {
console.log(this.getRawCsv(filePath));
[...]
}
getRawCsv(filePath) {
const results = [];
fs.createReadStream(filePath)
.pipe(parser())
.on("data", (data) => results.push(data));
// .on("end", () => {});
return results;
}
[...]
}
module.exports = CsvConverter;
I already tried to work with the .on("end", () => {}); callback, but I couldn't find a way to return the results array within the callback function.
Using Promises and the async/await functionality hasn't worked for me either but the reason on that might be, that I'm too unexperienced with this functionality.
Thanks for your help and the possible solutions :)
Kind Regards, Andreas