可读流中的visible.visibleEnded属性用于检查是否发出了结束事件。
用法:
readable.readableEnded
返回值:如果发出结束事件,则返回true,否则返回false。
下面的示例说明了Node.js中visible.visibleEnded属性的使用:
范例1:
// Node.js program to demonstrate the
// readable.readableEnded Property
// Include fs module
var fs = require("fs");
// Data to be displayed
var data = '';
// Create a readable stream
var readable = fs.createReadStream("input.text");
// Set the encoding to be utf8.
readable.setEncoding('UTF8');
// Handling stream event data and end
readable.on('data', function(chunk) {
data += chunk;
});
// End event
readable.on('end',function() {
console.log(data);
});
// Calling readableEnded property
readable.readableEnded;
输出:
read:hello true end!!
范例2:
// Node.js program to demonstrate the
// readable.readableEnded Property
// Include fs module
var fs = require("fs");
// Create a readable stream
var readable = fs.createReadStream("input.text");
// Set the encoding to be utf8.
readable.setEncoding('UTF8');
readable.on('readable', () => {
let chunk;
// Using while loop and calling
// read method with parameter
while (null !== (chunk = readable.read())) {
// Displaying the chunk
console.log(`read:${chunk}`);
}
});
// Calling readableEnded property
readable.readableEnded;
输出:
false read:hello
参考: https://nodejs.org/api/stream.html#stream_readable_readableended。
相关用法
- node.js Stream writable.writableHighWaterMark用法及代码示例
- node.js Stream readable.readableObjectMode用法及代码示例
- node.js Stream readable.readableLength用法及代码示例
- node.js Stream writable.writableFinished用法及代码示例
- node.js Stream writable.writableObjectMode用法及代码示例
- node.js Stream writable.writableLength用法及代码示例
- node.js Stream readable.readableHighWaterMark用法及代码示例
- node.js Stream writable.writableEnded用法及代码示例
- node.js Stream writable.destroyed用法及代码示例
- node.js Stream readable.destroyed用法及代码示例
- node.js Stream readable.readableEncoding用法及代码示例
- node.js Stream writable.writableCorked用法及代码示例
- node.js Stream readable.readableFlowing用法及代码示例
- node.js Stream writable.writable用法及代码示例
注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | Stream readable.readableEnded Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。