For instance, array.map
accepts a callback that returns a value.
const newArray = oldArray.map(item => doSomethingTo(item));
Promises also accept callbacks that return a value.
const requestPromise = $.ajax(...).then(res => doSomethingTo(res));
But the way I see most event emitters, they seem non-functional by design.
const emitter = new EventEmitter();
// emitter.on mutates the emitter to register event
// returns nothing in most cases.
emitter.on('SOME_EVENT', event => {
// Do something entirely irrelevant to the emitter.
// Do something that mutates something outside the callback.
// Returns nothing unlike array.map and promise callbacks.
// Return value is irrelevant to the caller.
});
// Executes callbacks but return values are irrelevant.
// Returns nothing.
emitter.trigger('SOME_EVENT');
So if my understanding of functional programming is correct, event emitter callbacks aren't pure nor transparent if this is the case.
Can event emitters be written in a more functional way? Or is this a limitation of the mechanism? Are event-driven and functional incompatible? How are event emitters done in other, more functional languages?
Event -> GlobalState -> GlobalState
(or equivalent) then sure.