Z
Z
zlodiak2017-11-23 13:05:52
Angular
zlodiak, 2017-11-23 13:05:52

Isn't it redundant to use rxjs/Observable?

Please tell me what is the point of using the rxjs library in angular, in particular rxjs / Observable?

In my opinion, you can do without it.
service:

import { Injectable } from '@angular/core';

import { Http } from '@angular/http';


@Injectable()
export class UsersService {

  constructor(private http: Http) { };

  getUsers(): Promise<Object> {
  	let users = this.http.get('../assets/json/users.json');
  	console.log(users);
  	return users.toPromise();
  };

}


component:
private getAllUsersData(): void {
  	this.usersService.getUsers().then(
      data => {   
        console.log(data);  
        this.allUsersData = JSON.parse(data._body);     
        console.log(this.allUsersData);             
      }, 
      err => {
        console.log('err')         
      })

  };


But in most examples that are on the Internet, for some reason, approximately the following code is used:
import { Injectable } from '@angular/core';

import { Http } from '@angular/http';

import { Observable } from 'rxjs/Observable';

import { Config } from '../config';


@Injectable()
export class VoteService {

  constructor(private http: Http) { };

  getVotes(): Observable<any> {
  	return this.http.get(Config.host + 'assets/json/feedback_0.json');
  };   

}


It is not clear to me why include extra libraries and therefore complicate

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Stroykin, 2017-11-23
@zlodiak

Link to an answer to a similar question or the same in Russian

D
Demian Smith, 2017-11-23
@search

In Angular, almost all (if not all) asynchronous operations return an Observable. By avoiding its use, you will not get any gains, but on the contrary, make your life much more difficult by breaking out of the framework ecosystem. Here is an article that helped me deal with rxjs https://habrahabr.ru/company/infopulse/blog/338910/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question