Pipe Works · Level 5 of 6

pipeline() cleans up

Source, transform, destination. One chunk is bad.

const { pipeline, Readable, Transform, Writable } = require('node:stream');const src = Readable.from(['1', '2', 'boom', '3']);const check = new Transform({  objectMode: true,  transform(chunk, enc, done) {    if (chunk === 'boom') return done(new Error('bad chunk'));    done(null, chunk);  },});const out = new Writable({  objectMode: true,  write(c, e, done) { console.log('out ' + c); done(); },});pipeline(src, check, out, (err) => console.log('pipeline: ' + (err ? err.message : 'ok')));

1. Predict the output

Tap the lines in the order you think they print.

  1. 1…
  2. 2…
  3. 3…