Skip to main content
edit
Source Link
Pankaj Parkar
  • 136.4k
  • 23
  • 241
  • 307
  1. Is this method of creating HTTP Service correct?

No, generally you shouldn't be consider using Promise with Observable.

  1. Basically getHttpReq (in class HttpService) Subscribes to an Observable & either resolves or rejects the promise depending on subscription's successful response or error. Am I correct?

There isn't any problem with your Promise implementation, but I don't understand why you're running away from using Observable. It has everything what you're looking for + they are lazy. Below is Observable implementation of the same in few lines of code.

Code

export class HttpService { constructor( private http: Http ) {} getHttpReq(url: string): Observable<any> { return this.http.get('http://192.168.0.189/longArray.js') .map(response => response.json()); }); } } //consumption getLongArrayTryOrFail(): Observable<boolean> { return this.httpService.getHttpReq('') .switchMapmap(response => true) .catch(error => Observable.throw(false)); }); } 
  1. Is this method of creating HTTP Service correct?

No, generally you shouldn't be consider using Promise with Observable.

  1. Basically getHttpReq (in class HttpService) Subscribes to an Observable & either resolves or rejects the promise depending on subscription's successful response or error. Am I correct?

There isn't any problem with your Promise implementation, but I don't understand why you're running away from using Observable. It has everything what you're looking for + they are lazy. Below is Observable implementation of the same in few lines of code.

Code

export class HttpService { constructor( private http: Http ) {} getHttpReq(url: string): Observable<any> { return this.http.get('http://192.168.0.189/longArray.js') .map(response => response.json()); }); } } //consumption getLongArrayTryOrFail(): Observable<boolean> { return this.httpService.getHttpReq('') .switchMap(response => true) .catch(error => Observable.throw(false)); }); } 
  1. Is this method of creating HTTP Service correct?

No, generally you shouldn't be consider using Promise with Observable.

  1. Basically getHttpReq (in class HttpService) Subscribes to an Observable & either resolves or rejects the promise depending on subscription's successful response or error. Am I correct?

There isn't any problem with your Promise implementation, but I don't understand why you're running away from using Observable. It has everything what you're looking for + they are lazy. Below is Observable implementation of the same in few lines of code.

Code

export class HttpService { constructor( private http: Http ) {} getHttpReq(url: string): Observable<any> { return this.http.get('http://192.168.0.189/longArray.js') .map(response => response.json()); }); } } //consumption getLongArrayTryOrFail(): Observable<boolean> { return this.httpService.getHttpReq('') .map(response => true) .catch(error => Observable.throw(false)); }); } 
Source Link
Pankaj Parkar
  • 136.4k
  • 23
  • 241
  • 307

  1. Is this method of creating HTTP Service correct?

No, generally you shouldn't be consider using Promise with Observable.

  1. Basically getHttpReq (in class HttpService) Subscribes to an Observable & either resolves or rejects the promise depending on subscription's successful response or error. Am I correct?

There isn't any problem with your Promise implementation, but I don't understand why you're running away from using Observable. It has everything what you're looking for + they are lazy. Below is Observable implementation of the same in few lines of code.

Code

export class HttpService { constructor( private http: Http ) {} getHttpReq(url: string): Observable<any> { return this.http.get('http://192.168.0.189/longArray.js') .map(response => response.json()); }); } } //consumption getLongArrayTryOrFail(): Observable<boolean> { return this.httpService.getHttpReq('') .switchMap(response => true) .catch(error => Observable.throw(false)); }); }