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:
javascript
Output:
javascript
Output:
readable.destroyedReturn 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;
Stream destroyed true errorExample 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;
Program completed!! false read: helloSo, 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