What are Rxjs operators and how to use operators in angular

 

Rxjs Operators are special type of functions that take input as an observable and then return an observable as an output that means Operators work with observable to make various kind of operations in angular or JavaScript.
there are around 110 operations in Rxjs where we can make various manipulation of collections e.g:
pipe() operator if is use to link operators together to make multiple functions into a single functions.

How to use Rxjs Operators

As we know Operators are special function which take input as argument and then emit final value as an Observable this means Operators are here to make changes in the stream so that we can fulfil the desired requirement. In an operation we can use multiple operators.
Example: How to make a sequence from 1 to 60 with time interval of 1 second.
Answer: If we have to emit an integer in a given time then we have interval operator which is used to emit numbers in sequence in a given timeframe.
Next we can use map operator to increase integer value every second so that we could have final output like we want. 
It is unstoppable for now until we give a final number to end with. For that we can use take() operator where it can only take given amount of operations.

  interval(1000)
  .map(x => x + 1) // to start from 1 instead of 0
  .map(x => console.log(x)) // do some logic here
  .take(60)
  .subscribe();

Types of Operators in Rxjs

There are total 8 types of operators in rxjs, like timer() operator is a creation of numbers so it is considered to be in creation category of Rxjs. Similarly there are 7 other types of operators we have in rxjs

  • Combination
  • Utility
  • Transformation
  • Multicasting
  • Filtering
  • error Handling
  • Creation
  • Combination

Summary

In this section , we focused on Rxjs Operators behavior , uses and how they work. so Using Rxjs Operator we can make our code structure more powerful and balanced because we have around 110 operator in rxjs where we can handle any kind of manipulation to our collections.

No comments:

Powered by Blogger.