Search code examples
rxjsrxjs-pipeable-operatorsrxjs-observables

How to run some code in an RxJS chain given there were no errors


I am trying to find a way to run some code only if there was no error in a given rxjs chain. Consider the following, is there something like the artificial NO_ERROR_OCCURED_RUN_HAPPY_PATH_CODE operator in rxjs?

private wrap(obs: Observable<any>): Observable<any> {
  return of(1).pipe(
    tap(() => this.spinner.startSpinner()),
    mergeMap(() =>
      obs.pipe(
        NO_ERROR_OCCURED_RUN_HAPPY_PATH_CODE(() => this.generic_success_popup()),
        catchError(this.handleError),            
      )
    ),
    finalize(() => this.spinner.stopSpinner())
  );
}

Solution

  • Basically almost all operator will be invoke if no error is thrown along the pipe, apart from finalize

      obs.pipe(
        tap(_=>console.log('no error, will run'),
        // throw some error
        map(_=>throwError('some error'),
        finalize(_=>console.log('will be called when there is error or upon observable complete')),
        tap(_=>console.log('this will not run')),
        catchError(this.handleError),           
      )