Node.js | Stream writable.uncork() Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// writable.uncork() method  
const stream = require('stream');
  
// Creating a stream and creating 
// a write function
const writable = new stream.Writable({
  
  // Write function with its 
  // parameters
  write: function(chunk, encoding, next) {
  
    // Converting the chunk of
    // data to string
    console.log(chunk.toString());
    next();
  }
});
  
// Writing data
writable.write('hi');
  
// Calling cork() function
writable.cork();
  
// Again writing some data
writable.write('hello');
writable.write('world');
  
// Calling uncork() function
writable.uncork();

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// writable.uncork() method  
const stream = require('stream');
  
// Creating a stream and creating 
// a write function
const writable = new stream.Writable({
  
  // Write function with its 
  // parameters
  write: function(chunk, encoding, next) {
  
    // Converting the chunk of
    // data to string
    console.log(chunk.toString());
    next();
  }
});
  
// Calling cork() function
writable.cork();
  
// Writing data
writable.write('hi');
  
// Calling cork() function
writable.cork();
  
// Again writing some data
writable.write('hello');
  
// Calling uncork function
// using nextTick()
process.nextTick(() => {
  
  // Calling uncork function
  writable.uncork();
  
  writable.uncork();
});

chevron_right


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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.