Node.js
Pipeline
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.
Exercise Write the pipeline function
Write function runPipeline(stages). Return stages.join(' -> '). Print console.log(runPipeline(['src', 'transform', 'sink'])).
Show example solution
function runPipeline(stages) {
return stages.join(' -> ');
}
console.log(runPipeline(['src', 'transform', 'sink']));
Test yourself
-
What advantage does stream.pipeline offer?
stream.pipeline(src, transform, sink, callback)
- Zincir hata olursa temizlenir ve bildirilir
- Hata sessizce yutulur
- Only connects two streams
- Hedef yok
Show answer
Zincir hata olursa temizlenir ve bildirilir — stream.pipeline cleans up the chain on error and notifies the callback.
-
Match each piece to its role
- src
- Produces data
- transform
- Processes data
- sink
- Writes data
- .join(' -> ')
- Joins stages
-
Complete the join call
return stages.____(' -> ');
Show answer
return stages.join(' -> ');
Next lesson: Custom stream