Node.js Stream writable.destroyed Property

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

Syntax:

writable.destroyed

Return Value: This property returns true if writable.destroy() method is called before it else it returns false.

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

Example 1:




// Node.js program to demonstrate the     
// writable.distroyed Property
  
// Accessing stream module
const stream = require('stream');
  
// Creating a stream and creating 
// a write function
const writable = new stream.Writable({
  
  // Write function with its 
  // parameters
  write: function(chunk, encoding, next) {
  
    // Converting the chunk of
    // data to string
    console.log(chunk.toString());
    next();
  }
});
  
// Writing data
writable.write('hi');
  
// Again writing some data
writable.write('hello');
  
// Calling destroy function
writable.destroy();
  
// Calling the Property
writable.destroyed;


Output:

hi
hello
true

Example 2:




// Node.js program to demonstrate the     
// writable.distroyed Property
  
// Accessing stream module
const stream = require('stream');
  
// Creating a stream and creating 
// a write function
const writable = new stream.Writable({
  
  // Write function with its 
  // parameters
  write: function(chunk, encoding, next) {
  
    // Converting the chunk of
    // data to string
    console.log(chunk.toString());
    next();
  }
});
  
// Writing data
writable.write('hi');
  
// Again writing some data
writable.write('hello');
  
// Calling the Property
writable.destroyed;


Output:

hi
hello
false

In the above example the output is false as writable.destroy() method is not called before calling the property writable.destroyed.

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