How do I know which event to listen to?
For example gulp.dest
fires the finish
event and then somewhat later the end
event. Some other streams only fire the finish
event. When I have a method that returns a stream (could be a read or write stream) and I execute the method, how can I wait for the returned stream to be finished? When do I have to register for the finish
and when for the end
event?
The gulp.dest
object is a Transform object - it handles reading from the (piped) source and writing to the destination. The 'end' event is emitted by the reader, while the 'finish' event is emitted by the writer.
If you are interested in ensuring the reading completes correctly, use .on('end')
. See: https://nodejs.org/api/stream.html#stream_event_end.
Readable streams will emit this once data has been completely consumed by the stream. So there may be cases when it doesn't fire, due to the internal logic of the transformation or to an error condition.
If it is the completion of the writing you are interested in, then use .on('finish')
instead. See: https://nodejs.org/api/stream.html#stream_event_finish.
Writable streams will emit this after data has been flushed to the underlying system. So in cases where expected read errors are handled, or where the transformation ends the reading early, this event should still be fired. As I understand it, this won't fire if the writing fails unexpectedly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With