Skip to content

Instantly share code, notes, and snippets.

@vnys
Last active February 2, 2016 13:04
Show Gist options
  • Save vnys/3e5ae5bc581e50120987 to your computer and use it in GitHub Desktop.
Save vnys/3e5ae5bc581e50120987 to your computer and use it in GitHub Desktop.
streams
const stream = require('stream');
let readable = new stream.Readable({
objectMode: true,
read: function() {
let arr = [ { a: 1 }, { b: 1 }, { c: 1 } ];
arr.forEach( item => { this.push(item) });
this.push(null);
}
}).on('end', function() {
console.log('Done reading');
});
let transform = new stream.Transform({
objectMode: true,
transform: function( chunk, encoding, next ) {
chunk.lol = true;
this.push(chunk);
next();
},
flush: function(done) {
done();
}
}).on('end', function() {
console.log('Done transforming');
});
let writable = new stream.Writable({
objectMode: true,
write: function( chunk, encoding, next ) {
console.log(chunk);
next();
}
}).on('finish', function() {
console.log('Done writing');
});
readable.pipe(transform).pipe(writable);
@vnys
Copy link
Author

vnys commented Feb 2, 2016

Note that the writable-stream emits «finish» instead of «end».

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment