Parse CSV at 1 line per second

I am trying to find libraries (to avoid re-creating the wheel) to read one line in the CSV file and clicking the value on the next process.

However, I need to read and process only once per second. Is there any way to do this on Node?

+4
source share
1 answer

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);
+3
source

All Articles