emittery icon indicating copy to clipboard operation
emittery copied to clipboard

`error` event for error handling

Open AuHau opened this issue 3 years ago • 4 comments

Currently, when you call emit() you can attache .catch() to the returned Promise for error handling. It has a limitation coming from Promise side as Promise can be only once resolved or rejected. But you can have multiple listeners for the event and if multiple listneres errors out then you will be notified only about the first one.

I would like to propose to have defined event name (error seems like good name and follows also similar event in the Node's EventEmitter) which would be called upon listener's error throw or rejection with the object passed with structure:

{
   event: string // name of the event of the listener that throw the error
   error: Error object
}

AuHau avatar Aug 28 '20 07:08 AuHau

What would the behavior be? Would it both reject on emit() and also emit an error event?

sindresorhus avatar Sep 03 '20 19:09 sindresorhus

Good question! I am not so sure myself, any suggestion?

I am leaning towards having both and leave it up to the user to choose what he needs base on his usecase...

AuHau avatar Sep 04 '20 05:09 AuHau

I think it would be weird to do both. Then the user would have to both listen to an error event and also try/catch the .emit(). I'm not really happy about the current situation or the proposed one, so I'm going to keep this open for a while, hoping that someone will propose a better solution.


If we would go for an error event, it should be a Symbol to not conflict with user events: emitter.on(Emittery.error, …).

sindresorhus avatar Sep 07 '20 10:09 sindresorhus

Ok. Well for having both, I did not really mean it as he would have to use both, but as he could choose based on his use-case (eq. if he has only one listener, than he can try/catch and if he would have multiple listeners then he can listen on the error event).

AuHau avatar Sep 07 '20 10:09 AuHau