Open In App

Node.js Stream readable.readableLength Property

Improve
Improve
Like Article
Like
Save
Share
Report

The readable.readableLength property in a readable Stream that is used to check the number of bytes in the queue which is ready to be read.

Syntax:

readable.readableLength

Return Values: It returns the number of bytes in the queue which are ready to be read.

Below examples illustrate the use of readable.readableLength property in Node.js:

Example 1:




// Node.js program to demonstrate the     
// readable.readableLength Property
   
// Accessing stream module
const stream = require('stream');
   
// Creating a Readable stream 
const readable = new stream.Readable("input.txt");
   
// Calling readable.readableLength 
// Property
readable.readableLength;


Output:

0

Example 2:




// Node.js program to demonstrate the     
// readable.readableLength 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 length
    console.log(`read: ${chunk.length}`);
     }
});
  
// Calling readableLength property
readable.readableLength;


Output:

0
read: 13

Reference: https://nodejs.org/api/stream.html#stream_readable_readablelength.



Last Updated : 12 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads