Node.js Stream readable.unpipe() Method
Last Updated :
12 Oct, 2021
The readable.unpipe() method in a Readable Stream is used to detach a Writable stream which was previously attached while using the stream.pipe() method.
Syntax:
readable.unpipe( destination )
Parameters: This method accepts single parameter destination which is the destination of writable stream to be detached.
Return Value: It returns stream.Writable i.e, the destination.
Below examples illustrate the use of readable.unpipe() method in Node.js:
Example 1:
const fs = require( 'fs' );
const readable = fs.createReadStream( "input.text" );
const writable = fs.createWriteStream( "output.text" );
readable.pipe(writable);
readable.unpipe(writable);
console.log( "done" );
|
Output:
done
Example 2:
const fs = require( 'fs' );
const readable = fs.createReadStream( "input.text" );
const writable = fs.createWriteStream( "output.text" );
readable.pipe(writable);
setTimeout(() => {
console.log( 'Stop writing to output.text.' );
readable.unpipe(writable);
console.log( 'close the file stream.' );
writable.end();
}, 2000);
console.log( "done" );
|
Output:
done
Stop writing to output.text.
close the file stream.
Reference: https://nodejs.org/api/stream.html#stream_readable_unpipe_destination.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...