Node.js Stream readable.destroyed Property
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:
const fs = require( "fs" );
const readable = fs.createReadStream( "input.txt" );
readable.on( 'readable' , () => {
let chunk;
while ( null !== (chunk = readable.read())) {
console.log(`read: ${chunk}`);
}
});
readable.on( 'error' , err => {
console.log(err);
});
readable.destroy( 'error' );
console.log( "Stream destroyed" );
readable.destroyed;
|
Output:
Stream destroyed
true
error
Example 2:
const fs = require( "fs" );
const readable = fs.createReadStream( "input.txt" );
readable.on( 'readable' , () => {
let chunk;
while ( null !== (chunk = readable.read())) {
console.log(`read: ${chunk}`);
}
});
console.log( "Program completed!!" );
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
Last Updated :
12 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...