In my module.js I have
var Stream = require('stream');
module.exports = function () {
var stream = new Stream();
stream.readable = true;
stream.emit('data', 'some stuff')
stream.emit('end')
return stream;
}
and in my index.js
var module = require('./module')
module().pipe(process.stdout)
substack’s example from the stream handbook is working just fine. Why doesn’t my code show anything in the command line?
Because you are emitting data before calling
pipe, and ‘data’ listener is attached after first ‘data’ event is firedEventEmitter’s calls are synchronous (as almost everything else non-IO in node.js)
A bit simplified version of
without EventEmitter could be rewritten as