Node.js streams triggers both end
and finish
events. What's the difference between both?
Asked
Active
Viewed 2.3k times
90

Simon Boudrias
- 42,953
- 16
- 99
- 134
1 Answers
160
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.

tiblu
- 2,959
- 1
- 22
- 22
-
2And also a `Transform` stream has both `end` and `finish` events. Indeed it extends `Duplex`. – Rocco Musolino Jul 20 '18 at 14:35
-
is this still valid in for the steams API after Node.js version 8? – Alexander Mills Jul 09 '19 at 18:57
-
1@AlexanderMills What did the docs say (https://nodejs.org/dist/latest-v12.x/docs/api/stream.html)? Also, you can try a simple test case with for example fs.ReadStream and fs.WriteStream - https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_class_fs_readstream. Feel free to contribute your findings. – tiblu Jul 10 '19 at 23:10
-
3`end` of data vs. `finished` working makes sense. A writable may have received all the data it is going to get, e.g. it received an `end` from upstream, but still be working. (I'm not sure but guessing that for a transform `finished` always comes after `end`?) – Craig Hicks Aug 06 '19 at 22:43
-
@tiblu The docs (even 5.x) contain opposite information. "`finish` is fired after `end`". As your answer has 160 upvotes I think I'm missing something here. Please clarify! – fishbone Mar 10 '23 at 07:26
-
@fishbone I don't state the exact event order in the original post? I would think the order is not guaranteed and depends on the exact streaming scenario at hand? – tiblu Mar 11 '23 at 19:28
-
1@tiblu I quoted this statement from the docs because that means they both fire (even for writable stream). In constrast your answer says either `finish` or `end` fires, depending on the stream type. That's the actual point of your answer and I can't find it in the docs. – fishbone Mar 16 '23 at 08:19
-
1@fishbone IF you refer to this https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_events_finish_and_end which states: `The **finish** event is fired after **stream.end()** is called and all chunks have been processed by stream._transform(), **end** is fired after all data has been output which is after the callback in stream._flush() has been called.`. Its about Transform streams. IF this is NOT what u are referring to, please add precise references (links). IF you still think both are fired for writable, write test code to prove it. Share the code. – tiblu Mar 18 '23 at 09:04
-
1Thanks, you are right, that was the missing point. Unfortunately SO doesn't let me remove the down vote – fishbone Apr 05 '23 at 09:32