Node.js Stream transform.destroy() Method
Last Updated :
03 Apr, 2023
The transform.destroy() method in a Readable Stream is used to destroy the transform stream and also emits an ‘error’ event optionally. Moreover, the transform stream releases any internal resources after this call is made.
Syntax:
transform.destroy( error )
Parameters: This method accepts single parameters error which emits an error event optionally.
Return Value: It emits an error event if any error is made in creating a stream else it only destroys the transform streams.
The below examples illustrate the use of transform.destroy() method in Node.js:
Example 1:
javascript
const zlib = require( "zlib" );
const transform = zlib.createGzip();
transform.destroy();
transform.destroyed;
|
Output:
true
Example 2:
javascript
const fs = require( "fs" );
const zlib = require( "zlib" );
const readable = fs.createReadStream( 'input.text' );
const writable = fs.createWriteStream( 'output.text' );
const transform = zlib.createGzip();
readable.pipe(transform).pipe(writable);
transform.destroy();
console.log( "done..." );
|
Output:
done...
Here, the transform stream is destroyed so the piping made is also removed.
Reference: https://nodejs.org/api/stream.html#stream_transform_destroy_error
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...