Skip to content

API Reference


API Reference / @dasch-ng/rxjs-operators / tapCatch

Function: tapCatch()

tapCatch<T, O, R>(options): UnaryFunction<Observable<T>, Observable<T | ObservedValueOf<O>>>

Defined in: tap-catch.ts:39

Combines the tap and catchError operators into a single convenient operator.

This operator is useful for executing side effects on successful emissions and handling errors in a single operation. It first applies a tap operation for side effects, then catches any errors that occur.

Type Parameters

T

T

The type of values emitted by the source observable

O

O extends ObservableInput<R>

The type of ObservableInput returned by the error handler

R

R = any

The type of values emitted by the error handler's observable

Parameters

options

Configuration object

error

(error, caught) => O

An error handler function that returns an ObservableInput

next

(value) => void

A function to execute for each emitted value (side effect)

Returns

UnaryFunction<Observable<T>, Observable<T | ObservedValueOf<O>>>

An operator function that performs side effects and catches errors

Example

typescript
import { of, throwError } from 'rxjs';
import { tapCatch } from '@dasch-ng/rxjs-operators';

throwError(() => new Error('Oops!'))
  .pipe(
    tapCatch({
      next: (value) => console.log('Got value:', value),
      error: (err, caught) => {
        console.error('Error occurred:', err);
        return of('fallback value');
      }
    })
  )
  .subscribe(console.log);
// Console output:
// Error occurred: Error: Oops!
// fallback value

Released under the MIT License.