This post was originally published on Jan 7, 2016 but has been updated to reflect the latest information.
In a previous post we showed how to leverage Observables, and especially their strength of composability to ease complicated async tasks. Today we want to take it one step further.
Want to see things in action first?
As a recap, we built a simple Wikipedia search demo consisting of a
WikipediaService to query a JSONP API.
We also built an
App component that uses this service and applies some Rx gymnastics to tame the user input, prevent duplicate requests and deal with out-of-order responses.
Thinking ahead we can refactor our code even further and let our API design leverage from the power of Observables.
Observables = Promises + Events (in a way!)
In a way Observables may be seen as the clever child of Events and Promises. Promises are first class objects that encapsulate the state of an asynchronous operation. But they are for singular operations only. A request is such an operation. You invoke a method, kicking off some async task and get a first class object that eventually will get you to the result of the operation (ignoring error handling for now).
Events on the other hand are for async operations that can continue to emit new values for an infinite duration. But Unfortunately they are traditionally not represented in a format that matches the criteria of a first class object. You can’t just pass an event of clicks around that skips every third click for instance.
Well, with Observables you can. You get the power of first class objects but without the limitations of singularity.
In fact, in a modern .NET language such as F#, which embraces Observables all the way down, every
IEvent<T> inherits from
IObservable<T>. Angular also went down this path and made
Smart service, dumb component
With that in mind: wouldn’t it be actually nice if we could save the component from dealing with all these edge cases? What if we just make the debounce duration configureable but let the rest of the complexity be handled by our
To let code speak we can transform our
WikipediaService into this.
Notice that the service still exposes the previous api as
rawSearch and builds a more clever
search API on top of it.
This dramatically simplifies our
See what happened? We just wire together event streams like lego blocks!
You can play around with the demo right here. Enjoy!
Angular 2 Master Class in Helsinki
Learn Angular 2 in our upcoming public training!Join now
Get updates on new articles and trainings.
Join over 1000 other developers who get our content first.
Custom Form Controls in Angular 2
Angular makes it very easy to create custom form controls. Read on to learn how to do it!
Protecting Routes using Guards in Angular 2
Angular's router enables protecting routes using guards and in this article we're going to discuss how to implement them.
Reactive Forms in Angular 2
Angular allows us to build forms in a model-driven fashion. In this article we're going to discuss what that looks...
Cold vs Hot Observables
In this article we are going to demystify what the term hot vs cold means when it comes to Observables....
Routing in Angular 2 revisited
Learn how to implement basic routing in your Angular 2 application using the latest and greatest APIs!
Component-Relative Paths in Angular 2
Component-relative enable developers to more easily create maintainable, reusable, portable components in Angular 2. Here's how!