asynchronous - using async.js with node.js streams -
i want use asyn.js limiting number of parallel io operations. have come across following example:
async.foreachlimit items, 5, ((item, next) -> request item.url, (error, response, body) -> console.log body next error) , (err) -> throw err if err console.log "all requests processed!"
but want use streams, this:
async.foreachlimit items, 5, ((item, next)-> stream = fs.createwritestream file request.get(item.url).pipe(stream)) , (err)-> throw err if err console.log "all requests processed!"
how place 'next' call when writestream done writing file?
you'll need bind readable stream's 'end'
event separate .pipe()
.
res = request.get(item.url) res.pipe(stream) res.on 'end', next
this lets bind 'error'
event:
res.on 'error', next
but, listen writable stream's 'finish'
event:
request.get(item.url) .on 'finish', next
Comments
Post a Comment