typescript - How to use an observable result and then return the observable -


using rxjs need 4 actions in angular service , component consumes service function:

-call function "ensurehascache():observable"

-call function "create():observable"

-take result of create() , assign local variable (this._companies.push (newentity);)

-then return observable in order consume on ui

this service code 4 actions take place:

public create (data:company):observable<company> {            return this.ensurehascache ().switchmap (res => {         return super.create (data);     }).switchmap ((newentity:company) => {                     this._companies.push (newentity);         return observableutils.fromresult (newentity);     }); }        

as can see in last switchmap need return observable have result. have call "fromresult". in way need create new observalbe ("fromresult" creates new observable) if dont need it.

is there more elegat way?

this code of fromresult:

public static fromresult<t> (result:t):observable<t> {     return new observable<t> (obs => {         obs.next (result);         obs.complete ();     }); } 

i using do operator of observables:

public create (data:company):observable<company> {          return this.ensurehascache ().switchmap (res => {     return super.create (data);   }).do((newentity:company) => {                 this._companies.push (newentity);   }); }  

Comments