Node.js Stream readable.readable 属性
readable.readable 属性是 Stream 模块的内置应用程序编程接口,用于检查调用 readable.read() 方法是否安全。
句法:
readable.readable
返回值:如果 readable.read() 方法被调用则返回 true,否则返回 false。
下面的示例说明了 Node.js 中readable.readable 属性的使用:
示例 1:
// Node.js program to demonstrate the
// readable.readable Property
// Include fs module
const fs = require('fs');
// Creating readable stream
const readable = fs.createReadStream("input.txt");
// Calling readable property
readable.readable;
输出:
true
示例 2:
// Node.js program to demonstrate the
// readable.readable Property
// Include fs module
const fs = require("fs");
// Constructing readable stream
const readable = fs.createReadStream("input.txt");
// Instructions for reading data
readable.on('readable', () => {
let chunk;
// Using while loop and calling
// read method
while (null !== (chunk = readable.read())) {
// Displaying the chunk
console.log(`read: ${chunk}`);
}
});
// Shows that the program
// ended
console.log("Program ends!!");
// Calling readable property
readable.readable;
输出:
Program ends!!
true
read: hello
参考: https://nodejs.org/api/stream.html#stream_readable_readable