mafintosh / csv-parser

Streaming csv parser inspired by binary-csv that aims to be faster than everyone else
MIT License
1.41k stars 134 forks source link

I'm not sure how to close a stream if I want to before the file is done being read. #187

Closed samjross closed 3 years ago

samjross commented 3 years ago

Documentation Is:

Please Explain in Detail...

this stackoverflow question has the asker using stream.unpipe(csv()); to close the stream if he's finished with it in the middle of the process, before it's done reading the rest of the file. I came to the npm docs to find out what the proper way is to close a stream and couldn't find any clarity on the question.

Your Proposal for Changes

Produce an example for closing the stream, like so:

fs.createReadStream('data.csv')
  .pipe(csv())
  .on('data', (data) => {
    if (someCondition) {
       // code to end/close the stream here
    }
  })
shellscape commented 3 years ago

Set a flag variable and just don't process any data as an alternative. Read up on Node streams to understand more on how streams work. You can close a stream in the data handler, but you're not guaranteed not to receive any more data events because streams are async.

This is not a documentation issue with csv-parser.