Node.js Stream writable.writableFinished Property
The writable.writableFinished property is set to true instantly before the emit of the ‘finish’ event.
Syntax:
writable.writableFinished
Return Value: It returns true if ‘finish’ event is called before it else it returns false.
Below examples illustrate the use of writable.writableFinished 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();
}
});
for (let i = 0; i < 5; i++) {
writable.write(`GfG, #${i}!`);
}
writable.on( 'finish' , () => {
console.log( 'All writes are now complete.' );
});
writable.end( 'This is the end\n' );
writable.writableFinished;
writable.destroy();
|
Output:
GfG, #0!
GfG, #1!
GfG, #2!
GfG, #3!
GfG, #4!
This is the end
All writes are now complete.
Example 2:
const stream = require( 'stream' );
const writable = new stream.Writable({
write: function (chunk, encoding, next) {
console.log(chunk.toString());
next();
}
});
writable.write( 'GfG' );
writable.writableFinished;
writable.destroy();
|
Output
GfG
In the above example the output is false as ‘finish’ event is not called before writable.writableFinished property.
Reference: https://nodejs.org/api/stream.html#stream_writable_writablefinished.
Last Updated :
12 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...