Last active
February 2, 2016 13:04
-
-
Save vnys/3e5ae5bc581e50120987 to your computer and use it in GitHub Desktop.
streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that the writable-stream emits «finish» instead of «end».