What is the difference between util.pump(streamA, streamB) and streamA.pipe(streamB)?
Asked Answered
P

3

6

The source code looks very similar: pump, pipe. Why would I use one instead of the other? Is one simply a better version of the other?

Plimsoll answered 15/3, 2012 at 19:11 Comment(0)
C
4

Stream.pipe is now the preferred method apparently since 0.3.x so try and use that when possible

sources:

https://groups.google.com/forum/?fromgroups#!topic/nodejs/FwdDQvAf4xM

https://mcmap.net/q/262365/-what-are-the-pros-and-cons-of-fs-createreadstream-vs-fs-readfile-in-node-js

Chivalric answered 3/4, 2012 at 14:10 Comment(0)
T
3

There are two significant difference between the two. The first is that the util.pump method can operate on two "stream-like" objects, while the Stream.prototype.pipe assumes that they are stream objects. The second is because of that assumption, the pipe can be chained ( streamA | streamB | streamC ) while the former can't (easily).

Tigre answered 15/3, 2012 at 20:15 Comment(0)
L
0

UPDATE: 28/04/2020

It's worth to say that pump is only necessary for Node.js 8.x or earlier, as for Node.js 10.x or later version, pipeline is introduced to replace for pump. This is a module method to pipe between streams forwarding errors and properly cleaning up and provide a callback when the pipeline is complete.

Leicestershire answered 28/4, 2020 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.