Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

nodejs
stream
uncork

What is the stream module uncork() in Node.js?

Sarvech Qadir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The uncork() method is an inbuilt method in Stream module Writable of NodeJS.

The uncork() method is used to flush all the data that becomes part of the buffer memory when the stream.cork() method is called.

Syntax:

writable.uncork() 

All the corked data is flushed and can be displayed in the output. This method does not take any parameters.

Code

Let’s look at a coding example using the .uncork() method.

uncork() needs to be called as many times as cork() is called; otherwise, entire data may not be flushed.

// accessing stream
const stream = require('stream');

// creating a Writable object along with a function.
const writable = new stream.Writable({

// Define a function to display the wrtiable data
write: function(value, encoding, next) {

	// dislaying the address of data saved in memory
	console.log(value);
	// displaying the actual data by converting it to string
    console.log(value.toString());
	next();
}
});

// write some data
writable.write('H');
writable.write('E');
writable.write('L');
writable.write('L');
writable.write('O');

// cork() function is called, all incoming data will be
// part of the buffer. 
writable.cork();

// All incoming data is corked and part of buffer memory.
writable.write('Welcome');
writable.write('to');
writable.write('Educative');

// Calling the `uncork method, the data with their address
// in printed in the console. 
writable.uncork();

RELATED TAGS

nodejs
stream
uncork

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring