Node.js Stream writable.writableEnded Property
The writable.writableEnded property is an inbuilt application programming interface of Stream module which is used to check the writable.end() method is being called or not.
Syntax:
writable.writableEnded
Return Value: It returns true if writable.end() method is being called before otherwise returns false.
Below examples illustrate the use of writable.writableEnded property in Node.js:
Example 1:
// Node.js program to demonstrate the // writable.writableEnded Property // Accessing stream module 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' ); // Again writing some data writable.write( 'GFG' ); // Calling end function writable.end(); // Calling writable.writableEnded // Property writable.writableEnded; // Calling destroy function //to display output writable.destroy(); |
Output:
hi GFG Writable { _writableState: WritableState { objectMode: false, highWaterMark: 16384, finalCalled: false, needDrain: false, ending: true, ended: true, finished: false, destroyed: true, 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] {}, _eventsCount: 0, _maxListeners: undefined }
Here, you can see that the ended property in the above example is being set to true.
Example 2:
// Node.js program to demonstrate the // writable.writableEnded Property // Accessing stream module 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' ); // Again writing some data writable.write( 'GFG' ); // Calling writable.writableEnded // Property writable.writableEnded; // Calling destroy function //to display output writable.destroy(); |
Output:
hi GFG Writable { _writableState: WritableState { objectMode: false, highWaterMark: 16384, finalCalled: false, needDrain: false, ending: false, ended: false, finished: false, destroyed: true, 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: false, errorEmitted: false, emitClose: true, autoDestroy: false, bufferedRequestCount: 0, corkedRequestsFree: { next: null, entry: null, finish: [Function: bound onCorkedFinish] } }, writable: true, _write: [Function: write], domain: null, _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined }
In the above example the ended property is set to false because writable.end() method is not called before calling the writable.writableEnded property.
Reference: https://nodejs.org/api/stream.html#stream_writable_writableended