Appendix D. RxJS essentials

 

Synchronous programming is relatively straightforward in that each line of your code is executed after the previous one. If you invoke a function in line 25 that returns a value, you can use the returned value as an argument for the function invoked in line 26.

Asynchronous programming dramatically increases code complexity. In line 37, you can invoke an asynchronous function that will return the value sometime later. Can you invoke a function in line 38 that uses the value returned by the previous function? The short answer is, “It depends.”

This appendix is an introduction to the RxJS 6 library, which can be used with any JavaScript-based app. It shines when it comes to writing and composing asynchronous code. Because Angular uses the RxJS library internally, we decided to add a primer to this book.

The first library of reactive extensions (Rx) was created by Erik Meijer in 2009. Rx.NET was meant to be used for apps written with Microsoft .Net technology. Then the Rx extensions were ported to multiple languages, and in the JavaScript world, RxJS 6 is the current version of this library.

Note

Though Angular depends on RxJS and can’t function without it, RxJS itself is an independent library that can be used in any JavaScript app.

Let’s see what being reactive means in programming by considering a simple example:

let a1 = 2;?
let b1 = 4;??
let c1 = a1 + b1;  // c1 = 6??

D.1. Getting familiar with RxJS terminology

D.2. Observable, observer, and subscriber

D.3. Creating observables

D.4. Getting familiar with RxJS operators

D.5. Using an observer API

D.6. Using RxJS Subject

D.7. The flatMap operator

D.8. The switchMap operator

D.9. Error handling with catchError

sitemap