RxJs - Join result of observable with other asynchronous values

Obtuse

I'm trying to merge 2 object arrays (each one I get from different apis and are of different types). The merge will be done on the last method but first I need to get both lists out of their observables. I need to do something like this:

this.result$ = this.firstValues$.pipe(
  function((firstValues) => firstValues.map(firstValue => this.secondApi.fetch(firstValue.id))),
  function((firstValues, secondValues) => this.fillFirstWithSecond(firstValues, secondValues))
);

Any ideas?

EDIT #1:

The function this.secondApi.fetch returns an Observable<SecondValue>.

Michael D

The question is quite obtuse. I assume by function you mean RxJS operators. In that case, you'd need to make quite a few changes. Although it would be much simpler to also attach a sample input with expected output.

this.result$ = this.firstValues$.pipe(
  switchMap((firstValues) =>
    forkJoin(                         // <-- trigger requests in parallel
      firstValues.map(firstValue =>   // <-- JS `Array#map` function
        this.secondApi.fetch(firstValue.id).pipe(
          map(secondValue => ({       // <-- include the `firstValue` in `secondApi`'s response
            firstValue: firstValue,
            secondValue: secondValue
          }))
        )
      )
    )
  ),
  map((values: {firstValue: any, secondValue: any}[]) => {
    this.fillFirstWithSecond(values.firstValues, values.secondValues)
  })
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

rxjs join and filter an observable based on values from a different observable

RXJS: Skipping values from observable triggered by other observable

rxJs callback with observable being asynchronous

RxJS Observable with asynchronous subscriber function

Make potentially asynchronous RxJS observable certainly asynchronous

rxjs - buffer emitted values until other observable emits, then emit as normal

RxJs Observable duplicate values

Initialize observable with the result of other observable

RxJS Observable result from an Observable's output

How to chain the result of observable to the below observable in rxjs

Get result from JOIN table by checking values in other rows

RXJS Emit first result and then wait for another observable

how to wait for an observable but return the previous observable result in rxjs?

Pass result of one RxJS observable into next sequential observable

Re-using an RxJS Observable to repeat an asynchronous call?

Join $graphLookup result with other collection

RXJS, how to change the result of each result from an observable?

Delay time between Observable values - RxJS

Separate observable values by specific amount of time in RxJS

RXJS Observable Transform Array To Multiple Values

RxJS get before/after emission values of Observable

How to get average of values in T-SQL from specific table and join it's result with other one?

Use data from one observable in another and then return result as other observable

rxjs: how to return the result from another observable from catchError

RxJS 5 Observable: does any result belong to a known set

rxjs: access the result of original observable after it was switchMap'ped

RxJS: Splitting an array result from Observable.fromPromise

Use RxJS pipe() to turn array into stream of asynchronous values in Angular

group by result not exist in join other tabel laravel?