Interview frequency: …
Short answer
Because pipeline manages error handling and automatically closes and destroys all connected streams if any error occurs, while .pipe() leaves the streams hanging.
Explanation
When using readable.pipe(writable), if a write error occurs, the readable stays open and the file handles or network sockets stay hanging. pipeline provides safe closing and destruction of all the participating streams in the event of any failure.
Code example
await pipeline(fs.createReadStream("src"), fs.createWriteStream("dest"));Common mistakes
- Using .pipe() without listening for error events on every stream in the chain.
Follow-up questions
- What are the four types of streams in Node.js and what is the function of each?
- What is the backpressure phenomenon in Node.js streams and how is it handled in engineering terms?