简体   繁体   English

Node.js 可读流事件“关闭”和“结束”之间的确切区别是什么?

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

I find the Node.js docs to be unclear.我发现 Node.js 文档不清楚。

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.在我看来,一旦 Readable 流完成发送其数据, 'close'事件就会触发,无论它是否已被完全消耗。

The 'end' event seems to only be fired if all of the data has been consumed from the Readable stream. 'end'事件似乎只有在可读流中的所有数据都已被消耗时才会被触发。

What is the actual difference?实际的区别是什么? When will each event be fired?每个事件什么时候触发?

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.当流及其任何底层资源(例如文件描述符)已关闭时,将发出 'close' 事件。 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.当流中没有更多的数据需要消耗时,将发出 'end' 事件。

In this case, the end will fire only when all the data of the stream has been consumed.在这种情况下,只有当流的所有数据都被消耗end才会触发end

They are quite different, and they are both useful.它们完全不同,而且都很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM