Node.js Stream writable.uncork() Method
Last Updated :
12 Oct, 2021
The writable.uncork() method is an inbuilt application programming interface of Stream module which is used to flush all the buffered data when stream.cork() method was called.
Syntax:
writable.uncork()
Parameters: This method does not accept any parameters.
Return Value: If this method is being called then the data which was being corked is again displayed in the output.
Below examples illustrate the use of writable.uncork() method in Node.js:
Example 1:
const stream = require( 'stream' );
const writable = new stream.Writable({
write: function (chunk, encoding, next) {
console.log(chunk.toString());
next();
}
});
writable.write( 'hi' );
writable.cork();
writable.write( 'hello' );
writable.write( 'world' );
writable.uncork();
|
Output:
hi
hello
world
In the above example, the data that was being corked is also returned in the output as uncork() function is called after that.
Example 2:
const stream = require( 'stream' );
const writable = new stream.Writable({
write: function (chunk, encoding, next) {
console.log(chunk.toString());
next();
}
});
writable.cork();
writable.write( 'hi' );
writable.cork();
writable.write( 'hello' );
process.nextTick(() => {
writable.uncork();
writable.uncork();
});
|
Output:
hi
hello
So, you need to call uncork() function the number of times you have called cork function. In the above example we have called cork() function two times so the uncork function is also called twice.
Reference: https://nodejs.org/api/stream.html#stream_writable_uncork
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...