Sitemap
Ravi

Ravi

RXJS Operators

3 min readOct 12, 2022

In this article, we’ll go through few of the commonly used rxjs operators. The following operators are illustrated in an Angular app. The syntax might differ for other frameworks. Let’s begin.

We’ll start with importing the operators first.

import { interval, map, throttleTime, filter, take} from ‘rxjs’;

In your component class, use a method to encapsulate the following code and test out the output of these operators.

// Create an observable that emits sequential numbers every 1s
const Observable = interval(1000);
// Create an observer which will subscribe to the observable
const Observer = {
next: function(value: number) {
console.log('Number: ', value);
}
}
// Subscribe to the observable and transform the output using map
const Example = Observable.pipe(
map(value => value * 2)
).subscribe(Observer)

In the code above, we use interval observer which is a creation operator and it emits sequential numbers. The pipe method on Observable is used to chain multiple operators. It passes the output of the first operator as an input to the next. The map operator applies a projection to each value emitted by the source Observable, and emits the resulting values as an Observable. In this case, all values are multiplied by 2 and returned. So the output that you get in the console is Number: 0 Number 2 Number 4 ...

Get Ravi’s stories in your inbox

Join Medium for free to get updates from this writer.

throttleTime operator emits the source Observable values on the output Observable when its internal timer is disabled, and ignores source values when the timer is enabled. Initially, the timer is disabled. As soon as the first source value arrives, it is forwarded to the output Observable, and then the timer is enabled. Let’s use it in our example by appending it in the pipe method.

const Example = Observable.pipe(
map(value => value * 2),
throttleTime(2000)
).subscribe(Observer)

As explained, as soon as throttleTime gets the first value(0) in this case, it’s timer gets enabled and it starts ignoring the values for the next 2 seconds / 2000ms. It will take the next value from map operator after that. So the output that you will see in the console will be Number: 0 Number: 6 Number: 12 Number: 18… and so on.

Let’s add a filter operator next. This operator takes values from the source Observable, passes them through a predicate function and only emits those values that yielded true.

const Example = Observable.pipe(
map(value => value * 2),
throttleTime(2000),
filter(num => num % 12)
).subscribe(Observer)

The output of the code above will be Number: 0 Number: 12 Number: 24 .. as it will only let the value which is when divided by 12 has the remainder 0 to pass through in this case.

With values getting printed in the console repeatedly, let’s clear up our console a bit. What if we only need to take the first six values of an Observable. In that case, we use take operator. It returns an Observable that emits only the first specified values emitted by the source Observable. If the source emits fewer than specified values then all of its values are emitted. After that, it completes, regardless if the source completes.

const Example = Observable.pipe(
map(value => value * 2),
throttleTime(2000),
filter(num => num % 12),
take(6)
).subscribe(Observer)

Until now, we had values getting logged in the console repeatedly. But now, only the first six values will be logged and the observable returned will complete. The output will be Number: 0 Number: 12 Number 24 Number: 36 Number: 48 Number: 60 .

Just like that, we have learned about a few basic rxjs operators. I will come up with some easy examples for some complex rxjs operators in my next article. Thank you.

--

--