<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shivamrajvir on Medium]]></title>
        <description><![CDATA[Stories by Shivamrajvir on Medium]]></description>
        <link>https://medium.com/@shivamrajvir?source=rss-e6fa75cc1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Xvi35p3c7qSKO8l0.jpg</url>
            <title>Stories by Shivamrajvir on Medium</title>
            <link>https://medium.com/@shivamrajvir?source=rss-e6fa75cc1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 00:53:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shivamrajvir/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Optimizing Angular Applications leveraging RxJS]]></title>
            <link>https://blog.searce.com/optimizing-angular-applications-leveraging-rxjs-a55230de2ad1?source=rss-e6fa75cc1------2</link>
            <guid isPermaLink="false">https://medium.com/p/a55230de2ad1</guid>
            <category><![CDATA[http-request]]></category>
            <category><![CDATA[rxjs]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shivamrajvir]]></dc:creator>
            <pubDate>Thu, 14 Nov 2019 15:41:05 GMT</pubDate>
            <atom:updated>2019-11-15T11:13:11.817Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are coming here from a Google Search, I am sure you know that Angular or basically Angular 2+ uses Observable pattern for it’s API call mechanism instead of promises compared to AngularJS, now ofcourse promises where quite easy to handle since they just closed themselves, but if you are familiar with the observable pattern, it doesn’t work that way. Allow me to explain the observable pattern in short.</p><p>Basically, an observable is a stream that allows data to transfer and it has manual subscribe() and unsubscribe() methods, so basically we have to close the observable for optimizing the application.</p><p>Lets take a look at some piece of code to understand this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IEBv6ipQ3oe-NYnWWyg6lg.png" /><figcaption>Component file</figcaption></figure><p>This is a component where we usually just inject a service and subscribe to a method that typically returns an observable event where we receive the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*C8FhUeHlDchfUNm66uckrQ.png" /><figcaption>service file</figcaption></figure><p>Here this will return the observable as it is when the data or error is received from the server, for URL argument I have just written here someUrl for the sake of example.</p><p>Although based on REST standards, the server only responds when a request is made, so once the request is made, and response has arrived, only another request will actually trigger a second response, else we will not receive any more responses from the server.</p><p>Now observables are pipable, and we have operators, typically RxJS operators are used to chain and optimize the observable pattern in Angular.</p><p>We have an observable take, now this requires an argument of type number, where we provide a number, the number of emissions we want, this is used usually where we just want one emission and in most API calls we can use this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tvkPDTgqSHJtHgVT7oTdjg.png" /><figcaption>Component file</figcaption></figure><p>Here we have added a pipe() that will basically allow us to use RxJS operators before subscribe method is triggered, so take(1) will automatically unsubscribe after the number of executions take place. (in this case 1)</p><p>Sometimes subscribe is like a firecracker, once is has been lit up, (started), it will chain and execute the way you want, now with asynchronous operations, say want to chain observables, or lets say execute some functions one after another with chaining, we need to follow certain steps to do so, otherwise it will just go away without waiting for the data and create undesired results.</p><h3>RxJS Operators</h3><p>RxJS operators are basically helpers that allow us to use existing functionalities. I will list few operators which are commonly used and can enhance your application.</p><p>To list a few of them I will use further, They are map, switchMap, filter, of, catchError. Let’s take a look at their usage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*2eGBdYa2pc5o_ad9VChYvQ.png" /><figcaption>service file</figcaption></figure><p>Allow me to explain the code, firstly a component will call makeAnApiCall function, which will check of data is saved in the variable, if not we will make an API call and populate that data, the reason to do this is to cache that data so dont have to make an API call for the same data again and again.</p><p>So in the first function, of operator is used, it will basically return any type of data but as an observable so we can get that data wherever that observable is called from.</p><p>map, is basically used to either alter any kind of data based on an array, which works here to but mainly to do some steps before the data is passed on in the chain.</p><p>catchError is used to catch any errors in the observable to handle those errors into the chaining. If they are uncaught is can be a huge problem for the application so we would not want that. throwError is not an operator but it will basically trigger the catchError mechanism for error handling.</p><p>switchMap is very similar to map operator but whenever we want to return an observable from the inside, we tend to use switchMap, using map here will present you with an error because map only works on normal type of data and not on observable type. SwitchMap is mainly used when we need to make a nested subscription chain as given in the function makeNestedApiCall</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/1*JKLdWFcqbWmGydnPtEXt6g.png" /><figcaption>component file</figcaption></figure><p>Here we can look at filter operator, this will basically filter all values if the given condition is not made, a subscription is a heavy operation and if you wish to filter some data that doesn’t need to be subscribed, this could be handy.</p><p>There are almost 108 operators provided by RxJS and they are adding new ones from time to time, however you can only find the functionality of them, how to apply them in your particular use case and provide a proper solution is something that only your core understanding can provide, sometimes it is also about experimenting. I encourage you to experiment, because that is how you learn, and the feeling of accomplishing a solution with experiment is something that is entirely different.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a55230de2ad1" width="1" height="1" alt=""><hr><p><a href="https://blog.searce.com/optimizing-angular-applications-leveraging-rxjs-a55230de2ad1">Optimizing Angular Applications leveraging RxJS</a> was originally published in <a href="https://blog.searce.com/">Searce</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>