Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the precise difference between Node.js Readable stream events 'close' and 'end'?

I find the Node.js docs to be unclear.

It appears to me that the 'close' event will fire once the Readable stream has finished sending its data, regardless if it has been completely consumed or not.

The 'end' event seems to only be fired if all of the data has been consumed from the Readable stream.

What is the actual difference? When will each event be fired?

like image 561
aryeh Avatar asked Jan 28 '26 21:01

aryeh


1 Answers

From the docs

The 'close' event is emitted when the stream and any of its underlying resources (a file descriptor, for example) have been closed. The event indicates that no more events will be emitted, and no further computation will occur.

As it says, it will be fired if one of its resources is closed, regardless of the data emitted. You might find it useful to clean up if it closed before all the data was sent or something similar.

The 'end' event is emitted when there is no more data to be consumed from the stream.

In this case, the end will fire only when all the data of the stream has been consumed.

They are quite different, and they are both useful.

like image 171
guzmonne Avatar answered Jan 30 '26 09:01

guzmonne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!