adalal
adalal

Reputation: 105

Parse CSV at the rate of 1 line per second

I've been trying to find libraries (to avoid re-inventing the wheel) to read a single line on a CSV file and pushing the value to a downstream process.

However, I need the read and processing to only happen once per second. Is there a way to do this on Node?

Upvotes: 4

Views: 147

Answers (1)

victorkt
victorkt

Reputation: 14552

I came across a similar problem recently, as I needed a way to read the file one line at a time and not move to the next until I was done processing the previous one.

I solved it by using promises, stream.pause(), and stream.resume(). You could do it like this:

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);

Upvotes: 3

Related Questions