Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between end and finish events in Node streams

Tags:

stream

node.js

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

like image 693
Simon Boudrias Avatar asked Feb 05 '15 01:02

Simon Boudrias


People also ask

What are buffers and streams in node JS?

A buffer is a temporary memory that a stream takes to hold some data until it is consumed. In a stream, the buffer size is decided by the highWatermark property on the stream instance which is a number denoting the size of the buffer in bytes. A buffer memory in Node by default works on String and Buffer .

What is stream chaining in node?

Chaining the Streams Chaining is a mechanism to connect the output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations. Now we'll use piping and chaining to first compress a file and then decompress the same.

Are node streams asynchronous?

Introduction. A stream is an abstraction of data in programming. The Node. js Stream API has been around for a long time and is used as a uniform API for reading and writing asynchronous data.


1 Answers

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

  • stream.Readable fires ONLY end and NEVER finish
  • stream.Writable fires ONLY finish and NEVER end

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.

like image 151
tiblu Avatar answered Oct 11 '22 20:10

tiblu