0

Is it a good practice to convert the observable object to a promise since observable can be used in almost all the occasions instead of promise?

I've started to learn angular recently and come across the below code snippet in a new project(Angular 5) at my workplace. This code snippet is used to load a list of data such as a customer list. This customer list data set is received as a one time action, not as a stream. Therefore it has no technical limitation to use promise. But I would like to know whether there are any drawbacks or limitations.

  getViewDataForPage(): Promise<any> {
    return this.commonDataService.getViewDataForPage(args_set)
      .toPromise()
      .catch(error => this._exceptionService.catchBadResponse(error));
  }


  //in commonDataService.ts
  getViewDataForPage(args_set): Observable<any> {
    /** logic goes here */
    return this.httpConnection.post(viewDataRequest, args);
  }
Hyyan Abo Fakher
  • 3,497
  • 3
  • 21
  • 35
Don D
  • 726
  • 1
  • 9
  • 19
  • Have a look here: https://stackoverflow.com/questions/37364973/promise-vs-observable – Jacopo Sciampi Sep 10 '18 at 12:16
  • Thanks for referring. Yes I have read this post earlier. Answers of it also have some conflicting ideas. That is why I have asked this question separately in order to learn any addition details on performance degrade, etc. Conflicting ideas: "If you want to use the reactive style, just use observables everywhere. If you have observables only you can easy compose. If you mix them it's not so clean anymore." "I am simply stating that I believe that people running into Observables mainly via http in NG2 have no real reason whatsoever to use Observables over Promises to make the calls." – Don D Sep 10 '18 at 12:39

2 Answers2

0

It depends on your requirement, technically observables are better than promises because they provide the features of Promise and more. With Observable, it doesn't matter if you want to handle none to multiple events.

Observables are cancelable ie., using unsubscibe() you can cancel an observable irrespective of its state.

Promises on the other hand deal with only 1 async event ie.., either it will resolve or reject if error occurs

A good place for Promise would be if you have a single even to process for example.

let connect=new Promise((resolve,reject)=>{

if( connection Passed){
   resolve("Connected");
 } else{
   reject("failed");
 }
}
Vaibhav
  • 1,481
  • 13
  • 17
  • 1
    My issue is in the case of subscribed to an observable is it a good practice to convert it to promise before use? You have mentioned "technically observables are better than promises". Does it mean that converting an observable to promise is not a good decision? – Don D Sep 11 '18 at 07:27
  • 1
    It is not a good decision, not a prominent decision, because you are getting multiple operators on the observable like map, filter, tap etc. Also, you might come to a situation where you have to cancel this observable, for some scenario where automation testing of your code is in the picture , and you might need to cancel and do a negative test performed @DonD – Vaibhav Sep 11 '18 at 08:41
0

You can use both. The Observable is used when you are going to receive different values during the time is more like when you are a subscriptor in a some magazine, when ever the mazagazine has new edition you will be notifier and the same happens to all subscriptors and everybody receives the new value or in this case the new magazine.

In the case of the Promise it is Async like an observable but it just happen ones.

Then if the following code will happens ones in this case is ok the promise

getViewDataForPage(): Promise<any> {
    return this.commonDataService.getViewDataForPage(args_set)
      .toPromise()
      .catch(error => this._exceptionService.catchBadResponse(error));
  }


  //in commonDataService.ts
  getViewDataForPage(args_set): Observable<any> {
    /** logic goes here */
    return this.httpConnection.post(viewDataRequest, args);
  }
Abel Valdez
  • 2,368
  • 1
  • 16
  • 33
  • Yes, as I have mentioned in question, I have understood that this will execute only once and there will be no errors. But I want to consider other aspects such as performance. – Don D Sep 11 '18 at 07:31