CodePark/ questions / Streams & backpressure
START PRACTICINGPRACTICE
Backend question · Node.js › Streams & backpressure

Why is pipeline() from node:stream/promises preferred over chaining .pipe() calls?

// A
src.pipe(gzip).pipe(dest);

// B
await pipeline(src, gzip, dest);
.pipe() does not forward errors or destroy the other streams, so failures can leak handles; pipeline() does both and reports one error

pipeline() forwards errors and cleans up every stream in the chain. It resolves or rejects once, with one error.

With
.pipe(), an error on one stream does not travel to the others. You need an error listener on every stream. Also, the other streams are not destroyed, which can leave file descriptors (open file handles) open.

"
.pipe() does not handle backpressure, pipeline() does" is wrong: both handle backpressure.