Search code examples
typescriptrxjsrxjs5

How to call forkJoin in switchOperator?


I have tried to call forkJoin in switchMap:

this.route.params.pipe(
  pluck('cadnum'),
  tap((cadnum) => {
    this.r1.repositoryModel.findallversionbycadnum.model.cadnum = cadnum;
    this.r2.repositoryModel.findallversionbycadnum.model.cadnum = cadnum;
    this.r3.repositoryModel.findallversionbycadnum.model.cadnum = cadnum;
  }),
  switchMap(() => forkJoin(
    egrn.parcels.versions.load(),
    egrn.premisses.versions.load(),
    egrn.rights.versions.load()).pipe(
      tap(([res1, res2, res3]) => {
        egrn.parcels.versions.set(res1);
        egrn.premisses.versions.set(res2);
        egrn.rights.versions.set(res3);
      }),
      catchError((error) => of(error)),
    ),
  ),
)).subscribe((response) => {
  console.log(response);
});

But it does not work, how to call forkJoin inside switchMap?


Solution

  • To forkJoin emit the results all egrn.parcels.versions.load(), egrn.premisses.versions.load(), egrn.rights.versions.load() have to be complete.

    You can try to switch forkJoin to combineLatest or finish the streams after the first emission with take(1), example: egrn.parcels.versions.load().pipe(take(1))