Home > Net >  How to turn a promise resolving with an observable to this observable?
How to turn a promise resolving with an observable to this observable?

Time:06-15

I have an API method which returns a promise resolving with an observable of Items. Using the means of RxJS how can I turn this promise into the observable of Items? In the language of code:

get items$(): Observable<Item> {
    const sourcePromise: Promise<Observable<Item>> = apiService.getItemsObservable();
    const sourceObservable: Observable<Observable<Item>> = from(sourcePromise);
    // ...
}

CodePudding user response:

You can solve this by using mergeMap. This will remove one layer of observables:

const levelDown$ = SourceObservable.pipe(
  mergeMap(val=>val)
);

  • Related