Node.js Writable Stream unpipe Event
The ‘unpipe’ event in a Writable Stream is emitted when the stream.unpipe() method is being called on a Readable stream by detaching this Writable from its set of destinations.
Syntax:
Event: 'unpipe'
Return Value: If the unpipe() method is being called then this event is emitted else it’s not emitted.
The below examples illustrate the use of the ‘unpipe’ event in Node.js:
Example 1:
javascript
const fs = require( "fs" );
const readable = fs.createReadStream( 'input.txt' );
const writable = fs.createWriteStream( 'output.txt' );
writable.on( "unpipe" , readable => {
console.log( "Unpiped!" );
});
readable.pipe(writable);
readable.unpipe(writable);
console.log( "Program Ended..." );
|
Output:
Unpiped!
Program Ended...
Example 2:
javascript
const fs = require( "fs" );
const readable = fs.createReadStream( 'input.txt' );
const writable = fs.createWriteStream( 'output.txt' );
writable.on( "unpipe" , readable => {
console.log( "Unpiped!" );
});
console.log( "Program Ended..." )
|
Output:
Program Ended...
So, here unpipe() function is not called so the unpipe event is not emitted.
Reference: https://nodejs.org/api/stream.html#stream_event_unpipe
Last Updated :
30 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...