Node 流中的结束和结束事件有什么区别 [英] What's the difference between end and finish events in Node streams
问题描述
Node.js 流同时触发 end
和 finish
事件.两者有什么区别?
Node.js streams triggers both end
and finish
events. What's the difference between both?
推荐答案
end
和 finish
是同一个事件,但是在不同类型的流上.
end
and finish
are the same event BUT on different types of Streams.
stream.Readable
只触发end
而从不finish
stream.Writable
只触发finish
而从不end
stream.Readable
fires ONLYend
and NEVERfinish
stream.Writable
fires ONLYfinish
and NEVERend
来源:https://nodejs.org/dist/latest-v5.x/docs/api/stream.html
为什么同一事件的命名不同?
我能想到的唯一原因是双工流 (stream.Duplex
),它同时实现了 stream.Readable
和 stream.Writable代码> 接口(https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_class_stream_duplex) 同时是可读和可写的流.要区分流上的读取结束和写入结束,您必须触发不同的事件.所以,对于双工流,
end
是读取结束,finish
是写入结束.
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.
这篇关于Node 流中的结束和结束事件有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!