Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Node.js Stream readable.destroyed Property

  • Last Updated : 12 Oct, 2021

The readable.destroyed property is an inbuilt application programming interface of Stream module which is used to check the readable.destroy() function is being called or not.

Syntax:

readable.destroyed

Return Value: It returns true if readable.destroy() method is being called otherwise returns false.

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

Example 1:






// Node.js program to demonstrate the     
// readable.destroyed 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}`);
  }
});
  
// Handling error event
readable.on('error', err => {
    console.log(err);
});
  
// Calling destroy method
// with parameter
readable.destroy('error');
  
// Displays that the stream is 
// destroyed
console.log("Stream destroyed");
  
// Calling readable.destroyed
// Property
readable.destroyed;

Output:

Stream destroyed
true
error

Example 2:




// Node.js program to demonstrate the     
// readable.destroyed 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}`);
  }
});
  
// Displays that the stream is 
// destroyed
console.log("Program completed!!");
  
// Calling readable.destroyed
// Property
readable.destroyed;

Output:

Program completed!!
false
read: hello

So, here readable.destroy() method is not called before readable.destroyed property so it returns false.

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!