Pipeline
- Node.js course
- Expert
- Lesson 28/47
- 5 min
Learn error-safe chaining with stream.pipeline.
What is a pipeline?
A pipeline chains multiple streams in order: source (src), transform and sink. Data leaves the source, passes through the transform and lands in the sink. stream.pipeline builds this chain and owns error handling end to end.
The old src.pipe(transform).pipe(sink) pattern swallowed errors silently; stream.pipeline cleans up the whole chain on any failure and reports it to a callback. A runPipeline(stages) function models the chain skeleton: it joins ['src', 'transform', 'sink'] with ' -> '. Output: src -> transform -> sink. This is a visual sketch of the chain; real streams move data, this one moves names.
Building the chain
The pipeline skeleton is built from the order of stages. The source produces data, the transform processes it and the sink writes it. This order shows up as a string in runPipeline(['src', 'transform', 'sink']).
Example: runPipeline(['src', 'transform', 'sink']) returns 'src -> transform -> sink'. Array.prototype.join combines every element of an array with a given separator. ['a', 'b'].join(' -> ') yields 'a -> b'. This simple model shows the chain's shape; in real stream.pipeline each stage is a stream object and data flows through. Joining the names is the shortest way to visualize the chain order.
Practice Write the pipeline function
Write function runPipeline(stages). Return stages.join(' -> '). Print console.log(runPipeline(['src', 'transform', 'sink'])).
Starter code
// Write the runPipeline function
// Join the stages array with ' -> ' and return it
// Print the call with ['src', 'transform', 'sink']
// write hereSample solution
function runPipeline(stages) {
return stages.join(' -> ');
}
console.log(runPipeline(['src', 'transform', 'sink']));Expected output
src -> transform -> sink
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What advantage does stream.pipeline offer?
stream.pipeline(src, transform, sink, callback)- Zincir hata olursa temizlenir ve bildirilir Correct answer
- Hata sessizce yutulur
- Only connects two streams
- Hedef yok
Explanation: stream.pipeline cleans up the chain on error and notifies the callback.
2 Match each piece to its role
| Term | Meaning |
|---|---|
src |
Produces data |
transform |
Processes data |
sink |
Writes data |
.join(' -> ') |
Joins stages |
3 Complete the join call
return stages.____(' -> ');
return stages.join(' -> ');
Explanation: join combines the array into a string.