我正在尝试使用fast-csv处理csv文件,下面是我的代码。
var stream = fs.createReadStream("sample.csv");
csv.fromStream(stream, {headers : true})
.on("data", function(data) {
console.log('here');
module.exports.saveData(data, callback)
})
.on("end", function(){
console.log('end of saving file');
});
module.exports.saveData = function(data) {
console.log('inside saving')
}我面临的问题是这个过程是不同步的。我看到的输出类似于
这里
这里
内部保存
内部保存
但是,我想要的是
这里
内部保存
这里
内部保存
我假设我们需要使用async.series或async.eachSeries,但不确定如何在这里使用。非常感谢您提供的任何信息
提前感谢!
发布于 2016-07-14 23:59:41
您可以暂停解析器,等待saveData完成,然后继续解析器:
var parser = csv.fromStream(stream, {headers : true}).on("data", function(data) {
console.log('here');
parser.pause();
module.exports.saveData(data, function(err) {
// TODO: handle error
parser.resume();
});
}).on("end", function(){
console.log('end of saving file');
});
module.exports.saveData = function(data, callback) {
console.log('inside saving')
// Simulate an asynchronous operation:
process.setImmediate(callback);
}https://stackoverflow.com/questions/38371241
复制相似问题