Skip to content

Instantly share code, notes, and snippets.

@voxpelli
Created September 16, 2014 14:22
Show Gist options
  • Save voxpelli/ee06e3e11eacafd0c6a2 to your computer and use it in GitHub Desktop.
Save voxpelli/ee06e3e11eacafd0c6a2 to your computer and use it in GitHub Desktop.
Node.js fail with EventEmitter, Promise and Exception
var events = require('events');
var Promise = require('Promise');
var foo = new events.EventEmitter();
foo.on('bar', function () {
console.log('Four');
throw new Error('Fail!');
});
console.log('One');
Promise.resolve().then(function () {
console.log('Three');
// Event handlers will be called synchronously
// which means they will be called within the Promise
// which means any exception from an event handler
// will just reject the Promise and not actually be thrown
// unless the Promise takes steps to rethrow it
foo.emit('bar');
console.log('Wont be printed');
}).catch(function (err) {
console.log('But this will be printed', err);
});
console.log('Two');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment