avatarTomas Trajan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4209

Abstract

"df08">The example above contains redundant handlers for both <code>next</code> and <code>error</code> handlers which <b>do exactly nothing </b>and could have been replaced by <code>null</code>.</p><blockquote id="d2d0"><p>Even better would be to pass the observer object with the <code>complete</code> handler implementation, omitting other handlers altogether!</p></blockquote><h1 id="53c8">The “.pipe()” and the operators</h1><p id="5cfa">As beginners are used to providing three arguments to subscribe, they often try to implement a similar pattern when using similar operators in the pipe chain.</p><p id="3826">RxJs operators, which are often confused with the <code>.subscribe()</code> handlers, are <code>catchError</code> and <code>finalize</code>. They both serve a similar purpose too — the only difference being that they are used in the context of the pipe instead of the subscription.</p><p id="0e63">In case we would like to react to the complete event of every subscription of the RxJs observable stream, we could implement <code>finalize</code> operator as a part of the observable stream itself.</p><blockquote id="255f"><p>That way we don’t have to depend on the developers to implement complete handlers in the every single .subscribe() call. Remember, the observable stream can be subscribed to more than once!</p></blockquote><figure id="e702"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_ltICn0yS2XMJNT6X2HEhg.png"><figcaption>Use the finalize operator to react to the complete event of the stream independently from the subscription. (Similar to tap)</figcaption></figure><p id="6d71">This brings us to the final and arguably most problematic pattern we may encounter when exploring various code bases: redundant operators added when trying to follow .subscribe() pattern in the .pipe() context.</p><figure id="ba6e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*yaz-4OUoxATbJdq82WMy4w.png"><figcaption></figcaption></figure><p id="e365">Also, we might encounter its even more verbose cousin…</p><figure id="05ef"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*OKlpdxGCZAr_RlojCVL1Eg.png"><figcaption>Stuff might get verbose…</figcaption></figure><p id="48df">Notice we have progressed from the original single line to the full nine lines of code which we have to read and understand when we want to fix a bug or add a new feature.</p><blockquote id="ec99"><p>Stuff might get even more complex when combined with more complex generic Typescript types, which can make the whole code block even more mysterious (and hence waste more of our time).</p></blockquote><h1 id="bd8d">Recapitulation</h1><ol><li>The <code>.subscribe()</code> method accepts both the observer object and inline handlers.</li><li>The observer object represents the most versatile and concise way to subscribe to an observable stream.</li><li>In case we want to go with the inline subscribe arguments (<code>next</code>, <code>error</code>, <code>complete</code>) we can provide <code>null</code> in place of a handler we don’t need.</li><li>We should make sure that we don’t try to repeat the <code>.subscribe()</code> pattern when dealing with <code>.pipe()</code> and operators.</li><li>Always strive to keep the code as simple as possible and remove unnecessary redundancies!</li></ol><h2 id="9770">That’s it! ✨</h2><blockquote id="32be"><p>I hope you enjoyed this article and will now have better understanding of how to subscribe to RxJs observables with clean, concise implementation!</p></blockquote><p id="7507">Please support this guide with your 👏👏👏 using the clap button and help it spread to a wider audience 🙏 Also, don’t hesitate to ping me if you have any questions using the article responses or Twitter DMs <a href="https://twitter.com/tomastrajan">@tomastrajan</a>.</p><figure id="d4f7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*psiw3z4G_TCPL43l.png"><figcaption>Welcome to the world of Angular excellence — <a href="https://www.angularexperts.ch/">angularexperts.ch</a></figcaption></figure><blockquote id="fdf1"><p><b><i>Do you find the information in this article useful?</i></b></p></blockquote><blockqu

Options

ote id="c849"><p><i>We are providing tailored expert support for developing of your Angular applications. Explore our wide range offers on <a href="https://www.angularexperts.ch/">angularexperts.ch</a></i></p></blockquote><p id="6f07" type="7">And never forget, the future is bright</p><figure id="ffe5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*J6eZnJvS8TUdmysZFA2VUw.jpeg"><figcaption>Obviously the bright future! (📸 by <a href="https://unsplash.com/photos/WV4B_aVj0aQ?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Xavier Coiffic</a>)</figcaption></figure><blockquote id="9ab9"><p>Starting an Angular project? Check out <a href="https://github.com/tomastrajan/angular-ngrx-material-starter">Angular NgRx Material Starter</a>!</p></blockquote><figure id="ebb0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jny3NETBblGCQW0EYKAMNQ.png"><figcaption>Angular NgRx Material Starter with built in best practices, theming and much more!</figcaption></figure><p id="5747">If you made it this far, feel free to check out some of my other articles about Angular and frontend software development in general…</p><div id="cae1" class="link-block"> <a href="https://hackernoon.com/%EF%B8%8F-the-7-pro-tips-to-get-productive-with-angular-cli-schematics-b59783704c54"> <div> <div> <h2>👨‍🔧️ The 7 Pro Tips To Get Productive With Angular CLI & Schematics 💪</h2> <div><h3>Angular Schematics is a workflow tool for the modern web — official introduction article</h3></div> <div><p>hackernoon.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*BmpWBv6AoWdbs3wffmIz_g.jpeg)"></div> </div> </div> </a> </div><div id="7e4a" class="link-block"> <a href="https://blog.angularindepth.com/the-best-way-to-unsubscribe-rxjs-observable-in-the-angular-applications-d8f9aa42f6a0"> <div> <div> <h2>The Best Way To Unsubscribe RxJS Observable In The Angular Applications!</h2> <div><h3>There are many different ways how to handle RxJS subscriptions in Angular applications and we’re going to explore their…</h3></div> <div><p>blog.angularindepth.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*YGzm0X1JG4k_KDF9pSOi-w.jpeg)"></div> </div> </div> </a> </div><div id="99f4" class="link-block"> <a href="https://readmedium.com/total-guide-to-angular-6-dependency-injection-providedin-vs-providers-85b7a347b59f"> <div> <div> <h2>Total Guide To Angular 6+ Dependency Injection — providedIn vs providers:[ ] 💉</h2> <div><h3>Let’s learn when and how to use new better Angular 6+ dependency injection mechanism with new providedIn syntax to make…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*UfgFDv5qOPGjzUjqzmBHCQ.jpeg)"></div> </div> </div> </a> </div><div id="3563" class="link-block"> <a href="https://blog.angularindepth.com/angular-question-rxjs-subscribe-vs-async-pipe-in-component-templates-c956c8c0c794"> <div> <div> <h2>The Ultimate Answer To The Very Common Angular Question: subscribe() vs | async Pipe</h2> <div><h3>Most of the popular Angular state management libraries like NgRx expose application state in a form of a stream of…</h3></div> <div><p>blog.angularindepth.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*5vQWDFbChZDlvONLHCrhxA.jpeg)"></div> </div> </div> </a> </div></article></body>

⚡ How to never repeat the same RxJs mistakes again⚡

Remember: .pipe() is not .subscribe()!

Look! A lightning tip! (Original 📸 by Max Bender)

This article is directed at the beginners trying to increase their RxJs knowledge but can also be a quick refresh or a reference to show to beginners for more experienced developers!

Today we are going to keep it short and straight to the point!

Currently I am working in a rather large organization quite a few teams and projects (more than 40 SPAs) that are in the process of migration to Angular and therefore also RxJs.

🤫 Wanna know how we can manage such a large environment without going full crazy 😵 Check out Omniboard!😉

This represents a great opportunity to get in touch with the confusing parts of RxJs which can be easy to forget once one masters the APIs and focuses on the implementation of the features instead.

The “.subscribe()” function

RxJs observable represent a “recipe” of what we want to happen. It’s declarative which means that all the operations and transformations are specified in their entirety from the get go.

An example of an observable stream could look something like this…

Example of the RxJs observable stream declaration

This RxJs observable stream will do literally nothing by itself. To execute it we have to subscribe to it somewhere in our codebase!

This subscription will log our greetings every odd minute

In the example above, we provided a handler only for the values emitted by the observable. The subscribe function itself accepts up to three different argument to the handle next value, error or complete event.

Besides that we could also pass in an object with the properties listed above. Such an object is an implementation of the Observer interface. The advantage of observer is that we don’t have to provide implementation or at least a null placeholder for the handlers we are not interested in.

Consider the following example…

In the code above, we are passing an object literal which contains only complete handler, the normal values will be ignored and errors will bubble up the stack.

And in this example, we are passing the handler of the next error and complete it as direct arguments of the subscribe function. All unimplemented handlers have to be passed as a null or undefined until we get to the argument we’re interested in.

As we can see, the inline argument style of implementation of a .subscribe() function call is positional.

In my experience, the inline arguments style is the one which is most common in various projects and organizations.

Unfortunately, many times we may encounter implementation like the following…

Example of redundant handlers often encountered in the “wild”

The example above contains redundant handlers for both next and error handlers which do exactly nothing and could have been replaced by null.

Even better would be to pass the observer object with the complete handler implementation, omitting other handlers altogether!

The “.pipe()” and the operators

As beginners are used to providing three arguments to subscribe, they often try to implement a similar pattern when using similar operators in the pipe chain.

RxJs operators, which are often confused with the .subscribe() handlers, are catchError and finalize. They both serve a similar purpose too — the only difference being that they are used in the context of the pipe instead of the subscription.

In case we would like to react to the complete event of every subscription of the RxJs observable stream, we could implement finalize operator as a part of the observable stream itself.

That way we don’t have to depend on the developers to implement complete handlers in the every single .subscribe() call. Remember, the observable stream can be subscribed to more than once!

Use the finalize operator to react to the complete event of the stream independently from the subscription. (Similar to tap)

This brings us to the final and arguably most problematic pattern we may encounter when exploring various code bases: redundant operators added when trying to follow .subscribe() pattern in the .pipe() context.

Also, we might encounter its even more verbose cousin…

Stuff might get verbose…

Notice we have progressed from the original single line to the full nine lines of code which we have to read and understand when we want to fix a bug or add a new feature.

Stuff might get even more complex when combined with more complex generic Typescript types, which can make the whole code block even more mysterious (and hence waste more of our time).

Recapitulation

  1. The .subscribe() method accepts both the observer object and inline handlers.
  2. The observer object represents the most versatile and concise way to subscribe to an observable stream.
  3. In case we want to go with the inline subscribe arguments (next, error, complete) we can provide null in place of a handler we don’t need.
  4. We should make sure that we don’t try to repeat the .subscribe() pattern when dealing with .pipe() and operators.
  5. Always strive to keep the code as simple as possible and remove unnecessary redundancies!

That’s it! ✨

I hope you enjoyed this article and will now have better understanding of how to subscribe to RxJs observables with clean, concise implementation!

Please support this guide with your 👏👏👏 using the clap button and help it spread to a wider audience 🙏 Also, don’t hesitate to ping me if you have any questions using the article responses or Twitter DMs @tomastrajan.

Welcome to the world of Angular excellence — angularexperts.ch

Do you find the information in this article useful?

We are providing tailored expert support for developing of your Angular applications. Explore our wide range offers on angularexperts.ch

And never forget, the future is bright

Obviously the bright future! (📸 by Xavier Coiffic)

Starting an Angular project? Check out Angular NgRx Material Starter!

Angular NgRx Material Starter with built in best practices, theming and much more!

If you made it this far, feel free to check out some of my other articles about Angular and frontend software development in general…

JavaScript
Rxjs
Typescript
Tech
Programming
Recommended from ReadMedium