I am having a slight issue when using async.queue with a filestream
- I have a scenario where my filestream will finish
- I set fileRead to true
- however the queue will be empty and already have called drain
- this then leads my "done" to never be called
what is the proper way to say "end the queue" after my filestream is "end" and the queue is empty?
var fs = require('fs')
, util = require('util')
, stream = require('stream')
, es = require('event-stream');
var async = require('async');
var fileRead = false;
var lineNr = 0;
var q = async.queue(function(task, callback) {
task(function(err, lineData){
responseLines.push(lineData);
callback();
});
}, 5);
var q.drain = function() {
if(fileRead){
done(null, responseLines);
}
}
var s = fs.createReadStream('very-large-file.csv')
.pipe(es.split())
.pipe(es.mapSync(function(line){
s.pause();
q.push(async.apply(insertIntoDb, line))
s.resume();
})
.on('error', function(err){
done(err);
})
.on('end', function(){
fileRead = true;
})
);
or is there a better use of async which would allow me to do this? async process line by line with the ability to exit early if one of the lines has errors
fileRead
to true. I think your problem is that thetask
function you're calling with each queue item is getting called and finished before theend
event gets called on your stream. – Alpinist