I want to apply an asynchronous transformation function on the value emitted by an observable.
@Injectable
export class ApiService{
constructor(private http: HttpClient){}
getSomething(url): Observable<any>{
return this.http.get(url);
}
}
In the code above, I want to apply a transformation function myFunc, which returns a promise, on the value emitted by this.http.get(url).
Normally I would use the map operator of RxJS, but since the transformation function returns a promise, I could not find a way to handle it.
For example, let my function be:
function myFunc(value){
return new Promise((resolve, reject) => {
// modify the value async
resolve(modifiedValue);
// ...
});
}
Is there an appropriate way to handle this task? I think the following is not suitable, am I right?
return this.http.get(url).map(myFunc);
Any help will be much appreciated.
Note: I'm using RxJS 5.5.2
{__zone_symbol__state: true, __zone_symbol__value : x}where is x is what I was expecting, but it's wrapped in another object as you see. I don't know what those zone symbols are though :)mapoperator with it. Can you change the mapping function to return an observable and then use theswitchMapoperator instead ofmap? Why is your mapping function async btw?return this.http.get(url).switchMap(x => Observable.fromPromise(myFunc(x)));