#rxjs : how to do

2 messages · Page 1 of 1 (latest)

floral sky
#
    this._cartographyV3Service
      .getCreatePollutantObs()            // (1)  Return pollutant created
      .pipe(
        takeUntilDestroyed(this._destroyRef),
        switchMap((pollutant: Pollutant) => {
          return this._pollutantService.getAllPollutants();
        })
      )
      .subscribe((pollutants: Pollutant[]) => {
        // OK, i get pollutants
        // BUT HERE, How I could do to recover the pollutant created in (1)
      })
clever silo
#

First, you should generally put the takeUntilDestroyed operator at the last position in the chain, otherwise you only unsubscribe from getCreatePollutantObs, but not from getAllPollutants.
That said: use the map operator to combine the two observable results:

    this._cartographyV3Service
      .getCreatePollutantObs()            // (1)  Return pollutant created
      .pipe(
        switchMap((pollutant: Pollutant) => {
          return this._pollutantService.getAllPollutants().pipe(
            map(pollutants => ({ createdPollutant: pollutant, allPollutants: pollutants}))
          );
        }),
        takeUntilDestroyed(this._destroyRef)
      )
      .subscribe(({ createdPollutant, allPollutants }) => {
        // ...
      });