In this blog, I'm going to explore the use of RxJS operators. As a beginner developer, we hear a lot about Promise/Observables/Subscription to call asynchronous services and perform data operations using traditional means, such as - loops, custom property mapper, and class models and so on. Instead, we can use various RxJS operators which are very easy and simple to write. In this blog, I will be demonstrating some of the real-time use cases of our day to day work and handling complex response in an easy way.
of() is used for converting the string/objects to Observables.
- import { Observable, of, from } from 'rxjs';
- ngOnInit() {
- const employee = {
- name: 'Rajendra'
- };
- const obsEmployee: Observable<any> = of(employee);
- obsEmployee.subscribe((data) => { console.log(data); });
- }

- ngOnInit() {
- const employee = {
- name: 'Rajendra'
- };
- const obsEmployee: Observable<any> = of('Rajendra Taradale');
- obsEmployee.subscribe((data) => { console.log(data); });
- }
- import { map } from 'rxjs/operators';
- ngOnInit() {
- const data = of('Rajendra Taradale');
- data
- .pipe(map(x => x.toUpperCase()))
- .subscribe((d) => { console.log(d); });
- }

- import { share} from 'rxjs/operators';
- getPosts(): Observable<any[]> {
- return this.http.get<any[]>('https://jsonplaceholder.typicode.com/users'));
- }
- setLoading(obs: Observable<any>) {
- this.loading = true;
- obs.subscribe(() => this.loading = false);
- }
- ngOnInit() {
- const request = this.getPosts();
- this.setLoading(request);
- request.subscribe(data => console.log(data));
- }

- getPosts(): Observable<any[]> {
- return this.http.get<any[]>
- ('https://jsonplaceholder.typicode.com/users').pipe(share());
- }
- getUsers(): Observable<any[]> {
- return this.http.get<any[]>('https://jsonplaceholder.typicode.com/users');
- }
- getPosts(): Observable<any[]> {
- return this.http.get<any[]>('http://jsonplaceholder.typicode.com/posts');
- }
- ngOnInit() {
- const reqPosts = this.getPosts();
- const reqUsers = this.getUsers();
- const reqPostsUser = reqPosts.pipe(
- switchMap(posts => {
- return reqUsers.pipe(tap(users => {
- console.log('Posts List ', posts);
- console.log('User List ', users);
- }));
- })
- );

DebounceTime and DistinctUntilChanged






Laxmidhar SahooPosted Jan 3, 2019, 10:53 PM
Thanks for the article .It is use full