Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created August 27, 2013 00:40
Show Gist options
  • Save cowboy/6348426 to your computer and use it in GitHub Desktop.
Save cowboy/6348426 to your computer and use it in GitHub Desktop.
Node.js - is there any way to guarantee the order here?
var stream = require('readable-stream');
var Transform = stream.Transform;
function Logger() {
Transform.call(this);
}
Logger.prototype = Object.create(Transform.prototype);
Logger.prototype._transform = function(chunk, encoding, done) {
done(null, chunk);
};
Logger.prototype.log = function(message) {
this.push(message + '\n');
};
var a = new Logger();
a.pipe(process.stdout);
var b = new Logger();
b.pipe(a);
a.log('order: 1, stream: a');
b.log('order: 2, stream: b');
a.log('order: 3, stream: a');
b.log('order: 4, stream: b');
// logs:
// order: 1, stream: a
// order: 3, stream: a
// order: 2, stream: b
// order: 4, stream: b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment