const { EventEmitter } = require('events')
const emitter = new EventEmitter()
const errFn = () => new Promise((resolve, reject) => {
reject(new Error('woot'))
})
errFn().catch((err) => {
emitter.emit('error', err)
})
Curiously, the above code produces the following output:
(node:75098) UnhandledPromiseRejectionWarning: Unhandled promiserejection (rejection id: 2): Error: woot
The promise rejection is handled, what isn't handled is the error event that is subsequently emitted. Expected output should be:
events.js:160
throw er; // Unhandled 'error' event
^
Error: woot
Node v6.6.0
This happens because the Promise completely swallows throws that occur even within the catch()
handler. Those end up as unhandled promise rejections internally. Since the built in Promise implementation in V8 does not give us appropriate visibility to see what's happening inside, we cannot trigger the unhandled error event.
Ah, I didn't realize that the error was actually thrown synchronously by emitter.emit('error', err)
and was catchable. So I guess it's not really a bug then? The annoying thing though is that there's no way to know that the error is because of an Unhandled 'error' event
.
You'd have to add another .catch
to catch the emitter error.
Ah, I didn't realize that the error was actually thrown synchronously by emitter.emit('error', err) and was catchable. So I guess it's not really a bug then? The annoying thing though is that there's no way to know that the error is because of an Unhandled 'error' event.
Right, that is the case at the moment. You can add an "error"
event handler to the emitter which will take care of it.
Alternatively, you can run node with --trace-warnings
which will tell you where the issue is.
As there is no action item at this point I'm going to close this, but feel free to follow up with a a comment, issue or a PR about how we can communicate these things better.
Most helpful comment
Right, that is the case at the moment. You can add an
"error"
event handler to the emitter which will take care of it.Alternatively, you can run node with
--trace-warnings
which will tell you where the issue is.As there is no action item at this point I'm going to close this, but feel free to follow up with a a comment, issue or a PR about how we can communicate these things better.