I'm currently using ES6 classes. I have a scenario where one emits a message, sending the values within an instance variable, and then I need to reset the variable after emitting.
So it looks something like the following (and works as intended, but looking for a more proper way of doing so):
// in class Ace
this.emit('someEvent', this.storage, () => {
this.storage = {};
});
// in class Bay
// Assuming `this.instanceAce` is an instance of class Ace
this.instanceAce.on('someEvent', (data, cb) => {
// do stuff with data
cb();
}
This works but the idea of providing a callback to an event listener is strange. This is assuming I would have exactly one listener for this event (which is currently true), but what happens in scenarios where I may have more than one listener?
Are there better alternatives to this solution? Something that does not seem to sort of ignore the purpose of the event model?