I am trying to do the following:
- Stream a csv file in line by line.
- Modify the data contained in each line.
- Once all lines are streamed and processed, finish and move on to next task.
The problem is .on("end")
fires before .on("data")
finishes processing each line. How can I get .on("end")
to fire after .on("data")
has finished processing all the lines?
Below is a simple example of what I am talking about:
import parse from 'csv-parse';
var parser = parse({});
fs.createReadStream(this.upload.location)
.pipe(parser)
.on("data", line => {
var num = Math.floor((Math.random() * 100) + 1);
num = num % 3;
num = num * 1000;
setTimeout( () => {
console.log('data process complete');
}, num);
})
.on("end", () => {
console.log('Done: parseFile');
next(null);
});
Thanks in advance.
pipe
anddata
are not meant to be used together? Maybe add adata
event handler to whateverparser
is rather than here? – Swetlanaimport parse from 'csv-parse';
I'll give it a try – Newfeld