Created
September 16, 2014 14:22
-
-
Save voxpelli/ee06e3e11eacafd0c6a2 to your computer and use it in GitHub Desktop.
Node.js fail with EventEmitter, Promise and Exception
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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