在 node.js 中,我有一个读取流,我希望重新格式化并写入数据库。由于读取流快而写入慢,node.js 队列可能会随着写入队列的增加而不堪重负(假设流是 gb 的数据)。如何强制读取等待代码的写入部分,以便在没有阻塞的情况下不会发生这种情况?
var request = http.get({
host: 'api.geonames.org',
port: 80,
path: '/children?' + qs.stringify({
geonameId: geonameId,
username: "demo"
})
}).on('response', function(response) {
response.setEncoding('utf8');
var xml = new XmlStream(response, 'utf8');
xml.on('endElement: geoname ', function(input) {
console.log('geoname');
var output = new Object();
output.Name = input.name;
output.lat = input.lat;
output.lng = input.lng;
output._key = input.geonameId;
data.db.document.create(output, data.doc, function(callback){
//this is really slow.
}
// i do not want to return from here and receive more data until the 'create' above has completed
});
});