Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check whether callback is actually a function #49

Open
patrickvuarnoz opened this issue Jun 5, 2024 · 0 comments
Open

Check whether callback is actually a function #49

patrickvuarnoz opened this issue Jun 5, 2024 · 0 comments

Comments

@patrickvuarnoz
Copy link

I ran into a problem where I got an error message like TypeError: Cannot read properties of undefined (reading 'apply') in E.emit. This was due to a code change where I changed the name of an event handler but forgot to change it also in the call to emitter.on().

It took me quite some time to find the problem in the code as it is not obvious from the error message which event handler (call to on()) caused the problem.

It would be great if the emitter would check in the on() and once() methods whether callback is actually a function and if not throw an error. This way it would directly indicate which portion of the code was trying to register a non-callable event handler. Something like:

if (typeof callback !== 'function') throw new Error('Callback is not a function');

The issue is easy to reproduce:

emitter.on('some-event', undefined);
emitter.emit('some-event');

emitter.on('some-event', 'string');
emitter.emit('some-event');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant