I am using requests module in nodejs to make a request and I am streaming response I got. This response stream is piped to csv parser and populating records onto an array. Once I get a preset count of records, I want to end csv parsing and close the response stream. How do I properly cleanup response stream. Here is the pseudo code
var stream = request.get(url);
stream.pipe(csvParser);
var count = 15;
csvParser.on("readable",function(){
while(record = csvParser.read()){
if(records.length<count){
records.push(record);
} else {
csvParser.end();
//stream.close();
//stream.unpipe();
// stream.destroy();
}
}
});
csvParser.on("error",function(err){
console.log("Error",err.message);
})
csvParser.on("finish",function(){
//console.log("records",records);
console.log("done");
})
when I try stream.close() , it's saying undefined method. What's the correct way of cleaning it up..?