I recently ran into a similar problem, as I needed a way to read the file one line at a time, and not move on to the next until I finished processing the previous one.
I solved this using promises, stream.pause()and stream.resume(). You can do it as follows:
const Promise = require('bluebird');
const fs = require('fs');
const byline = require('byline');
function readCSV(file, callback) {
let stream = fs.createReadStream(file);
stream = byline.createStream(stream);
stream.on('data', (line) => {
stream.pause();
Promise.resolve(line.toString())
.then(callback)
.then(() => setTimeout(() => stream.resume(), 1000));
});
}
readCSV('file.csv', console.log);
source
share