The writable.cork() method is an inbuilt application programming interface of Stream module which is used to write every data into the buffer memory. When we use stream.uncork() or stream.end() methods then the buffer data will be flushed.
Syntax:
writable.cork()
Parameters: This method does not accept any parameters.
Return Value: If this method is used then the data written after this method is not displayed in the output as these data are stored in the memory and can be again shown using some other specific methods.
Below examples illustrate the use of writable.cork() method in Node.js:
Example 1:
javascript
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' );
|
Output:
hi
Here, in the above example the data written before cork() method is only displayed and the data written after it is corked i.e. stored in the memory.
Example 2:
javascript
const stream = require( 'stream' );
const writable = new stream.Writable({
write: function (chunk, encoding, next) {
console.log(chunk.toString());
next();
}
});
writable.write( 'hi' );
writable.write( 'hello' );
writable.write( 'world' );
writable.cork();
|
Output
hi
hello
world
In the above example, the cork() method is written at last so, none of the data is being stored in the memory. Therefore, all the written data is displayed in the output.
Reference: https://nodejs.org/api/stream.html#stream_writable_cork
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Aug, 2022
Like Article
Save Article