Posts

Exploring RxJs Operators - filter

In a previous post I gave an introduction to the observable and included some information on piping operators but I feel like each operator deserves its own post even if the post is going to be reasonably small. In this post, I thought we would look over the filter operator given that it will probably be one of the most commonly used operators for many developers. As the name suggests filter filters values from the stream of values that an observable emits, let's start with an example: 1| this.keyboardEvents.pipe( 2|      filter(event => event.key === 'Enter'), 3|      tap(() => console.log('Enter pressed!') 4|  ); Let's talk about each line of code, line 1 is simple enough; the keyboardEvents  observable emits a KeyboardEvent . Line 2 is where we will focus most of our attention on, the filter operator, in this case, will filter out any KeyboardEvent objects that do not have 'Enter' as its key. Line 3 just verifies what I have alr

Let's talk Observables.

If you have spent any length of time with either front-end development or the world of NodeJS you will be no stranger to the difficulties of handling asynchronous code, hopefully, you also have a fair few tools to help you out. First off we have our 'trusty' callback function but this soon becomes a complete nightmare once we start having to nest callbacks, the code spirals into a complete mess where you will be lucky to find any level of readability or resilience to bugs. This brings us on to Promises, they are very useful, still useful in fact but again experience has told us that chaining Promises quickly becomes a nightmare, code starts to become overly complex and without a lot of due care and diligence the code is straight up unreadable. There are plenty of examples out there of the above, you have probably thought of a few places in your code base or a past code base where the above assertions are all too real, so what is the solution? Observables right?