<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Operator - Inero Software - Software Consulting</title>
	<atom:link href="https://inero-software.com/tag/operator/feed/" rel="self" type="application/rss+xml" />
	<link>https://inero-software.com/tag/operator/</link>
	<description>We unleash innovations using cutting-edge technologies, modern design and AI</description>
	<lastBuildDate>Fri, 08 Apr 2022 09:29:29 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://inero-software.com/wp-content/uploads/2018/11/inero-logo-favicon.png</url>
	<title>Operator - Inero Software - Software Consulting</title>
	<link>https://inero-software.com/tag/operator/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">153509928</site>	<item>
		<title>RxJs for beginners (5 most useful operators)</title>
		<link>https://inero-software.com/rxjs-for-beginners-5-most-useful-operators/</link>
		
		<dc:creator><![CDATA[Waldemar Korłub]]></dc:creator>
		<pubDate>Thu, 26 Nov 2020 12:45:44 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Company]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[angular]]></category>
		<category><![CDATA[DOM event]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[Http request]]></category>
		<category><![CDATA[Observable]]></category>
		<category><![CDATA[Operator]]></category>
		<guid isPermaLink="false">https://sandbox-www.devel.inero.com.pl/?p=3159</guid>

					<description><![CDATA[<p>This time we’re going to take a look at RxJs operators, what they do, and how you should (or shouldn’t) use it. In case you decide that in fact you want to use it then you can read the rest of the article where I go over most common use&#8230;</p>
<p>Artykuł <a href="https://inero-software.com/rxjs-for-beginners-5-most-useful-operators/">RxJs for beginners (5 most useful operators)</a> pochodzi z serwisu <a href="https://inero-software.com">Inero Software - Software Consulting</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div style="padding-top: 1rem;">This time we’re going to take a look at RxJs operators, what they do, and how you should (or shouldn’t) use it. In case you decide that in fact you want to use it then you can read the rest of the article where I go over most common use cases and usage examples.</div>
<p>WARNING: I assume that you know basics of asynchronous programming, if not then please read this before going any further: <a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Concepts">Asynchronous Concepts</a></p>
<h3><span style="color: #511b73;"><b>So, what’s RxJs?</b></span></h3>
<p>According to the documentation:</p>
<blockquote><p>RxJS is a library for composing asynchronous and event-based programs by using observable sequences. It provides one core type, the Observable, satellite types (Observer, Schedulers, Subjects) and operators inspired by Array#extras (map, filter, reduce, every, etc) to allow handling asynchronous events as collections.</p></blockquote>
<p>In simple words, RxJs is a library which helps you in working with asynchronous tasks, events and streams and it does that by sharing some useful classes like aforementioned <em>Observable</em> and a ton of cool operators (due to my desire of keeping this post relatively short I am going to present only 5 operators that I find the most useful in my everyday work). Actually, RxJs has been so helpful to the Angular community that the Angular team decided to include it into Angular itself with the 6th version.</p>
<p>We are going to focus on <em>Observables</em> this time. <em>Observable</em> is a class that allows you to listen for events. To retrieve data from an <em>observable</em> you need to subscribe to it, when you do that it listens for events happening, so when they happen, you get the data. You can get more details on <em>observables</em> here: https://rxjs.dev/guide/observable.</p>
<p>Now let’s discuss 3 most common situations where developers are using <em>Observables</em></p>
<h4><span style="color: #511b73;"><b>Http Requests</b></span></h4>
<p>All HtttpClient methods are returning Observables and that is of course because it takes time for a server to process and return the data (or just return in case you’re sending a <em>get</em> request). That means that http request operation is asynchronous.</p>
<h4><span style="color: #511b73;"><b>DOM events</b></span></h4>
<p>Observables enable you to easily listen for DOM events like <em>click</em> or <em>keyup</em>. To listen to them you should use the <em>fromEvent</em> function that creates an <em>observable</em> accordingly. Here’s how this looks like:</p>
<p>fromEvent(document, &#8216;keyup&#8217;);</p>
<p>(Remember that if you want to receive the actual data you should subscribe to it by adding .subscribe() before semicolon)</p>
<h4><span style="color: #511b73;"><b>Stores</b></span></h4>
<p>Another very common use case for using <em>Observables</em> is using them with some kind of store, like NgRx or NgXs. Stores like that usually share observables in order to keep watch over the state of the application.</p>
<p>Now since you know the very basics like why and how, let’s get to the useful interesting stuff that can be one with RxJs operators.</p>
<h3><span style="color: #511b73;"><b>RxJs o</b></span><span style="color: #511b73;"><b>perators</b></span></h3>
<p>In our examples we are going to use simplest observable possible:</p>
<pre>const obs$ = from([1, 2, 3]); (dollar sign at the end of the variable is a naming convention used to singlaize that the variable is indeed an observable)
</pre>
<h4><span style="color: #511b73;"><b>How to use operators?</b></span></h4>
<p>Easiest way to use operators is by putting them inside a pipe. Pipe runs a function for every stream element passing by the operator, and operators are the functions that can be used within it.</p>
<h5><span style="color: #511b73;"><b>first</b></span></h5>
<p><em>First</em> operator is the first operator that we’re going to talk about (got the pun?). Most operators have self-describing titles and this case is no different. <em>First</em> operator basically allows you to listen for the first returned value of the <em>observable</em> and then stop (note that it is completely useless combined with Http requests that return the value once anyways)</p>
<pre>obs$.pipe(first()).subscribe(numbr =&gt; console.log(number));
</pre>
<p>Code above prints only 1 because it’s the first event emitted, 2 and 3 are ignored.</p>
<h5><span style="color: #511b73;"><b>tap</b></span></h5>
<p>Another useful operator worth mentioning is <em>tap</em>. Tap allows you to perform some kind of side effect for every event emission. For example you can log something or assign data to a variable (note: if you are using .subscribe it is basically the same thing).</p>
<pre>obs$.pipe(tap(numbr =&gt; console.log(number))).subscribe();
</pre>
<p>Code above prints 1, 2 and 3 because <em>tap</em> is triggered for every emitted value.</p>
<h5><span style="color: #511b73;"><b>map</b></span></h5>
<p><em>Map</em> is working the same way as in arrays. So it basically allows you to change structure of returned data. For example, let’s try to multiply every value returned by 2</p>
<pre>obs$.pipe(map(numbr =&gt; numbr * 2)).subscribe(value =&gt; console.log(value));
</pre>
<p>The code prints 2, 4 and 6 just as expected.</p>
<h5><span style="color: #511b73;"><b>startWith</b></span></h5>
<p><em>StartWIth</em> allows you to decide what first value is going to be. It is useful when you want to trigger the observable before the first event emission.</p>
<pre>obs$.pipe(startWith(999)).subscribe(value =&gt; console.log(value));
</pre>
<p>Printed values are: 999, 1, 2, 3.</p>
<h5><span style="color: #511b73;"><b>switchMap</b></span></h5>
<p><em>SwitchMap</em> is used mainly in typeahead cases but not limited to them. What it does &#8211; it cancels previous request when a new event arrives. In real life case you would for example listen for changes on the input and filter a table while typing (previous result is not relevant).<br />
To present switchMap fully &#8211; this time we are going to use a different example. <em>Observable</em> is going to be created out of DOM event keyup.</p>
<pre>const another$ = timer(1000);
fromEvent(document, 'keyup').pipe(
switchMap(() =&gt; another$)
).subscribe(() =&gt; console.log('log'));
</pre>
<p>What happens here is: After you click a button on keyboard, wait 1000 ms and then log a message. Now what happens ,if you click button 10 times quicker? Only one message is going to be logged, and that is because switchMap cancels previous timers leaving only the most recent one.</p>
<p>We went over 5 most useful operators in my opinion, described what RxJs is and discussed common use cases. I hope that this article will help you in your next project and interest you in exploring RxJs more 🙂</p>
<p><a href="https://inero-software.com/contact-us/"><strong><span style="color: #800080;">Inero Software</span></strong></a> provides knowledge and expertise on how to successfully use cutting edge technologies and data to shape corporate digital products of the future.</p>
<p><span data-contrast="auto">In the <a href="https://inero-software.com/category/blog/company/"><strong><span style="color: #800080;">blog post</span></strong></a> section, you can find other articles about our projects and more!</span><span data-ccp-props="{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}"> </span></p>
<p>Artykuł <a href="https://inero-software.com/rxjs-for-beginners-5-most-useful-operators/">RxJs for beginners (5 most useful operators)</a> pochodzi z serwisu <a href="https://inero-software.com">Inero Software - Software Consulting</a>.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3159</post-id>	</item>
	</channel>
</rss>
