Callbacks as a Way of Life
In any given day in the browser, you might be using
- DOM and Custom Events
- Timeouts and Intervals (setTimeout, setInterval)
- Ajax Requests
And with the introduction of HTML5, we have even more possibilities for rich interactions adding in, each with their own way of expressing asynchronous/evented behavior.
- Scheduling (requestAnimationFrame)
- File API
- Web Sockets
- Web Workers
Making the Case for Composition
But, what if we want to go a little deeper than that?
- What if we want to throttle data such as key up events?
- What if we want to filter the data before querying another service?
- What about composition? Can I combine events together?
- The list goes on…
When we start addressing those concerns, then we quickly lose sight of the actual logic required for simply taking text and querying a service. It goes from a pretty simple solution to something a bit harder to understand.
This is just a simple example, but the idea is that we can combine together both events which yield zero to many values, with single return methods such as Ajax requests, in a single API. Patterns such as Promises go only so far with assurances of composition in a single way of dealing with completion, progress and errors, but it doesn’t address the very core issue of event composition.