You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to the documentation, one of the features of EventEmitter2 is that it supports async listeners. The documentation does not mention that listeners should never reject.
Rejections in listeners result in Node process crash. For example:
{ emitted: true }
event listener called
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "async error".] {
code: 'ERR_UNHANDLED_REJECTION'
}
Replacing reject('from async') with resolve('from async') results in
{ emitted: true }
event listener called
success!
Changing promisify and async listener options does not seem to affect this.
It seems that EventEmitter2 does not handle any promise rejections (at least I cannot find any mentions of catch in the source code). Since the value returned by emit is simply true, it seems that there is no way for caller of emit to handle any rejections either.
So the only way to not crash Node process is to handle all rejections in the listener, and to use EventEmitter2, for example, like this:
This is probably a duplicate of #298, I didn't notice it at first because I was searching by "reject" keyword.
Still, the documentation should probably mention that only emitAsync (the one that returns promises from listeners, allowing the caller to handle rejections) should be used in conjunction with async listeners.
According to the documentation, one of the features of EventEmitter2 is that it supports async listeners. The documentation does not mention that listeners should never reject.
Rejections in listeners result in Node process crash. For example:
produces the following output:
Replacing
reject('from async')
withresolve('from async')
results inChanging
promisify
andasync
listener options does not seem to affect this.It seems that EventEmitter2 does not handle any promise rejections (at least I cannot find any mentions of
catch
in the source code). Since the value returned byemit
is simplytrue
, it seems that there is no way for caller ofemit
to handle any rejections either.So the only way to not crash Node process is to handle all rejections in the listener, and to use EventEmitter2, for example, like this:
This is a very significant caveat, and it should probably be mentioned in the documentation that async listeners should never ever throw or reject.
(This is not an issue with sync listeners, because errors thrown by sync listeners can be caught by caller of
emit
as ordinary sync errors.)The text was updated successfully, but these errors were encountered: