Node.js Stream writable.write() Method
The writable.write() method is an inbuilt application programming interface of Stream module which is used to write some data to the Writable stream. The callback function is called once the data has been completely handled.
Syntax:
writable.write( chunk, encoding, callback)
Parameters: This method accepts three parameters as mentioned above and described below:
- chunk: It is an optional data to write. The value of chunk must be a string, buffer or Uint8Array. For object mode, the chunk value may be anything other than null.
- encoding: It holds the encoding value if chunk is a string value.
- callback: It is an optional callback function for stream.
Return Value: It returns false if the ‘drain’ event is emitted before this method otherwise returns true.
Below examples illustrate the use of writable.write() 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( "GfG" , "utf8" , () => {
console.log( "CS-Portal!" );
});
|
Output:
GfG
true
CS-Portal!
Example 2:
const stream = require( 'stream' );
const writable = new stream.Writable({
write: function (chunk, encoding, next) {
console.log(chunk.toString());
next();
}
});
writable.write( 'GeeksforGeeks' );
|
Output:
GeeksforGeeks
true
Reference: https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback
Last Updated :
12 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...