The writable.end() method is an inbuilt application programming interface of Stream module so that no more data can be written to the Writable anymore. The arguments chunk and encoding are optional which will permit one final new chunk of data to be written instantly before closing the stream. Moreover, the optional callback function is added as a listener for the ‘finish’ event of the Writable stream.
Syntax:
writable.end( 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 the data written before calling this method and if the end() method has a chunk of new data then that is also returned at the end.
Below examples illustrate the use of writable.end() 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( 'hi' );
writable.end( "last data" , "utf8" , () => {
console.log( "Writable stream ended!" );
});
|
Output:
hi
last data
Writable {
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: true,
ended: true,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 2,
prefinished: true,
errorEmitted: false,
emitClose: true,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree:
{ next: null,
entry: null,
finish: [Function: bound onCorkedFinish] } },
writable: false,
_write: [Function: write],
domain: null,
_events:
[Object: null prototype] {
finish: { [Function: bound onceWrapper] listener: [Function] } },
_eventsCount: 1,
_maxListeners: undefined }
Writable stream ended!
Example 2:
const stream = require( 'stream' );
const writable = new stream.Writable({
write: function (chunk, encoding, next) {
console.log(chunk.toString());
next();
}
});
writable.write( 'hi' );
writable.end( "last data" , "utf8" , () => {
console.log( "Writable stream ended!" );
});
writable.write( 'GfG' );
|
Output:
hi
last data
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_stream_writable.js:248:12)
at Writable.write (_stream_writable.js:296:5)
at /home/runner/LuxuriousLegitimateObservation/index.js:30:10
at Script.runInContext (vm.js:133:20)
at Object. (/run_dir/interp.js:156:20)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)Writab
le stream ended!
Here an error is displayed as write() method is called after end() method which is not possible.
Reference: https://nodejs.org/api/stream.html#stream_writable_end_chunk_encoding_callback
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 :
12 Oct, 2021
Like Article
Save Article