Open In App

Node.js Stream writable.uncork() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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();


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:




// 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();
});


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



Last Updated : 12 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads