Search code examples
angulartypescriptrxjspipecombinelatest

Rxjs Convert Observable<any>[] to Observable<any[]>


Let's imagine that we have two interfaces:

export interface IA{
   something: string;
   myprop: number;
}

export interface IB{
   myprop: number;
}

I have a method that should call endpoint that returns IA object from a backend, then it should call another endpoint and then combine both results into IA object. Previously I was doing something like this:

GetA():Observable<IA>{
    return this.httpClient
        .get<IA>('somewhere')
        .concatMap(a=>Observable.combineLatest(
           Observable.of(a), 
           GetB(a)
        ))
        .map([a,b]=>combineSomehowAandB(a,b))
}

But now, with new version of rxjs I'm forced to use .pipe(operators[]) instead. How to implement the same with pipe()? I tried like this, but it doesn't work:

GetA():Observable<IA>{
    return this.httpClient
        .get<IA>('somewhere')
        .pipe(
           concatMap(a=>[Observable.of(a), GetB(a)]),
           combineLatest(),
           map([a,b]=>combineSomehowAandB(a,b))
         );
}

Thanks in advance.


Solution

  • It looks like you just didn't rewrite your original chain into RxJS 6 correctly:

    return this.httpClient.get<IA>('somewhere')
      .pipe(
        concatMap(a => combineLatest(of(a), GetB())),
        map(([a,b]) => combineSomehowAandB(a,b)),
      );
    

    Using combineLatest() by itself without any parameter is useless.