Node.js streams triggers both end
and finish
events. What's the difference between both?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
end
and finish
are the same event BUT on different types of Streams.
stream.Readable
fires ONLYend
and NEVERfinish
stream.Writable
fires ONLYfinish
and NEVERend
Source: https://nodejs.org/dist/latest-v5.x/docs/api/stream.html
Why the different naming of the same event?
The only reason I could think of is because of duplex streams (stream.Duplex
), which implement both stream.Readable
and stream.Writable
interfaces (https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_class_stream_duplex) are readable and writable stream at the same time. To differentiate between end of reading and end of writing on the stream you must have a different event fired. SO, for Duplex streams end
is end of reading and finish
is end of writing.