📜  Node.js Stream readable.readableEnded 属性

📅  最后修改于: 2022-05-13 01:56:33.818000             🧑  作者: Mango

Node.js Stream readable.readableEnded 属性

可读 Stream 中的readable.readableEnded 属性用于检查是否发出结束事件。

句法:

readable.readableEnded

返回值:如果发出结束事件,则返回true,否则返回false。

以下示例说明了 Node.js 中readable.readableEnded 属性的使用:

示例 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。