Skip to content

Instantly share code, notes, and snippets.

@jpmx
Created July 27, 2013 14:16
Show Gist options
  • Save jpmx/6094993 to your computer and use it in GitHub Desktop.
Save jpmx/6094993 to your computer and use it in GitHub Desktop.
var Util         = require('util');
var EventEmitter = require('events').EventEmitter;

function myfunc(cb) {
  EventEmitter.call(this);
  this.emit('end');
  cb();
}
Util.inherits(myfunc, EventEmitter);

var go = new myfunc(function() {
  console.log("Got CB");
});

go.on('end', function() {
  console.log("END by emit");
});

Expected output:

END by emit
Got CB

Actual output:

Got CB

If I add a setTimeout, I get the emit call

var Util         = require('util');
var EventEmitter = require('events').EventEmitter;

function myfunc(cb) {
  EventEmitter.call(this);
  self = this;
  setTimeout((function() { self.emit('end'); }), 2000);
  cb();
}
Util.inherits(myfunc, EventEmitter);

var go = new myfunc(function() {
  console.log("Got CB");
});

go.on('end', function() {
  console.log("END by emit");
});

Expected output:

Got CB
END by emit

Actual output:

Got CB
END by emit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment