Node 流中的结束和结束事件有什么区别 [英] What's the difference between end and finish events in Node streams

查看:13
本文介绍了Node 流中的结束和结束事件有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js 流同时触发 endfinish 事件.两者有什么区别?

Node.js streams triggers both end and finish events. What's the difference between both?

推荐答案

endfinish 是同一个事件,但是在不同类型的流上.

end and finish are the same event BUT on different types of Streams.

  • stream.Readable 只触发 end 而从不 finish
  • stream.Writable 只触发 finish 而从不 end
  • stream.Readable fires ONLY end and NEVER finish
  • stream.Writable fires ONLY finish and NEVER end

来源:https://nodejs.org/dist/latest-v5.x/docs/api/stream.html

为什么同一事件的命名不同?

我能想到的唯一原因是双工流 (stream.Duplex),它同时实现了 stream.Readablestream.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆