RxJS Tutorial
Selected Reading
- RxJS - Discussion
- RxJS - Useful Resources
- RxJS - Quick Guide
- RxJS - Working with RxJS & ReactJS
- RxJS - Working with RxJS & Angular
- RxJS - Working with Scheduler
- RxJS - Working with Subjects
- RxJS - Working with Subscription
- RxJS - Operators
- RxJS - Observables
- RxJS - Latest Updates
- RxJS - Environment Setup
- RxJS - Overview
- RxJS - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
RxJS - Working with Subscription
RxJS - Working with Subscription
When the observable is created, to execute the observable we need to subscribe to it.
count() operator
Here, is a simple example of how to subscribe to an observable.
Example 1
import { of } from rxjs ; import { count } from rxjs/operators ; let all_nums = of(1, 7, 5, 10, 10, 20); let final_val = all_nums.pipe(count()); final_val.subscribe(x => console.log("The count is "+x));
Output
The count is 6
The subscription has one method called unsubscribe(). A call to unsubscribe() method will remove all the resources used for that observable i.e. the observable will get canceled. Here, is a working example of using unsubscribe() method.
Example 2
import { of } from rxjs ; import { count } from rxjs/operators ; let all_nums = of(1, 7, 5, 10, 10, 20); let final_val = all_nums.pipe(count()); let test = final_val.subscribe(x => console.log("The count is "+x)); test.unsubscribe();
The subscription is stored in the variable test. We have used test.unsubscribe() the observable.
Output
The count is 6Advertisements