如何在 Node.js 中关闭一个可读流?
var input = fs.createReadStream('lines.txt');
input.on('data', function(data) {
// after closing the stream, this will not
// be called again
if (gotFirstLine) {
// close this stream and continue the
// instructions from this if
console.log("Closed.");
}
});
这将比以下内容更好:
input.on('data', function(data) {
if (isEnded) { return; }
if (gotFirstLine) {
isEnded = true;
console.log("Closed.");
}
});
但这并不能阻止阅读过程...
stream.destroy()
方法。 - joeytwiddlereadable.push(null) && readable.destroy();
吗? - Alexander Mills