Node.js Stream 可读. readable flow Property

原文:https://www . geesforgeks . org/node-js-stream-readable-flow-property/

可读流中的可读. readable 属性,用于检查流是否处于流动模式。

语法:

readable.readableFlowing 

返回值:如果流处于流动模式,则返回真,否则返回假。

下面的例子说明了在 Node.js 中可读. readableflow 属性的使用:

例 1:

// Node.js program to demonstrate the     
// readable.readableFlowing Property  

// Include fs module
var fs = require("fs");
var data = '';

// Create a readable stream
var readerStream = fs.createReadStream('input.txt');

// Handling data event
readerStream.on('data', function(chunk) {
   data += chunk;
});

// Calling readableFlowing property
readerStream.readableFlowing;

输出:

true

例 2:

// Node.js program to demonstrate the     
// readable.readableFlowing 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 with parameter
  while (null !== (chunk = readable.read())) {

    // Displaying the chunk
    console.log(`read: ${chunk}`);
  }
});

// Calling readable.readableFlowing
// Property
readable.readableFlowing;

输出:

false

参考:https://nodejs . org/API/stream . html # stream _ readable _ flow