我有 JSON 如下customers.json
{
"customers":[
{ "name": "customer1"},
{ "name": "customer2"},
{ "name": "customer3"}
]
}
var fs = require('fs'),
JSONStream = require('JSONStream'),
es = require('event-stream');
async function run(){
var getStream = function () {
var jsonData = 'customers.json',
stream = fs.createReadStream(jsonData, { encoding: 'utf8' }),
parser = JSONStream.parse('customers.*');
return stream.pipe(parser);
};
var arr = [];
getStream()
.on('data', function (chunk) {
arr.push(chunk);
})
.on('end', function () {
console.log('All the data in the file has been read' + arr.length);
})
.on('close', function (err) {
console.log('Stream has been Closed');
});
console.log('end run()');
}
async function main(){
run().then(function(){
console.log('In then');
}).catch(function(){
console.log('In catch');
})
}
main();
在输出中为什么在“end”、“close”之前打印“In then”。事件。
如何在“结束”、“关闭”事件之后获得“In then”或“In Catch”。
我如何以同步方式执行 run() 方法。