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:
const stream = require( 'stream' );
const writable = new stream.Writable({
write: function (chunk, encoding, next) {
console.log(chunk.toString());
next();
}
});
writable.write( 'hi' );
writable.write( 'GFG' );
writable.end();
writable.writableEnded;
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:
const stream = require( 'stream' );
const writable = new stream.Writable({
write: function (chunk, encoding, next) {
console.log(chunk.toString());
next();
}
});
writable.write( 'hi' );
writable.write( 'GFG' );
writable.writableEnded;
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
Last Updated :
12 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...