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

查看:86
本文介绍了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接口(

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天全站免登陆