我使用nodeJs csv模块解析10000记录大csv和每个记录,即 .on('record',func),我必须实现一个耗时的逻辑。但节点不会等到我完成。我该如何处理?文件谈到http://www.adaltas.com/projects/node-csv/ pause()和resume()。但是我在哪里使用它?
var csv = require('csv');
var fs = require('fs');
csv()
.from.stream(fs.createReadStream(__dirname+'/sample.in'))
.to.path(__dirname+'/sample.out')
.transform( function(row){
row.unshift(row.pop());
return row;
})
.on('record', function(row,index){
//Time consuming logic goes here
})
.on('end', function(count){
console.log('Number of lines: '+count);
})
.on('error', function(error){
console.log(error.message);
});
答案 0 :(得分:2)
我终于有了这个工作。感谢node-csv开发人员
//Read through the csv file one row at a time
csv()
.from(__dirname+'data.csv', { columns: true })
.transform(function(row, index, next){
setTimeout(function(){
console.log(row);
next()
}, 3000); //replace setTimeout with any fn and call next() in the callback
}, {parallel: 1})
.on("end", function (count) {
var finalcount = count;
var logtext = 'SUMMARY: Total count: ' + finalcount + '\n'
+ 'Error count:' + errorcount + "("+errorArray+")"
+ '\n' + 'Success count:' + successcount;
fs.appendFile(config.logfilename, '\n'+ logtext, function (err) {
console.log(logtext);
});
console.log("done", count);
});