Skip to content

Instantly share code, notes, and snippets.

@spasiu
Created November 4, 2015 01:57
Show Gist options
  • Save spasiu/d967333b9c4778d80326 to your computer and use it in GitHub Desktop.
Save spasiu/d967333b9c4778d80326 to your computer and use it in GitHub Desktop.
readable streams
const Readable = require('stream').Readable;
const util = require('util');
function Reader() {
Readable.call(this);
this.arr = [0,1,2,3,4,5,6,7,8,9,10,11];
}
util.inherits(Reader, Readable);
Reader.prototype._read = function() {
let element = this.arr.pop()
return this.push(element? element.toString(): null);
};
let reader = new Reader();
reader.setEncoding('utf8');
reader.on('data', chunk => console.log(chunk));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment