MatthiasKunnen / rxjs-throw-if

RxJS operator which throws an error when a predicate is met

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

npm version

rxjs throw-if
^7.0.0 ^3.0.0
^6.0.0 ^2.0.0

ThrowIf

RxJS operator which throws an error if the given predicate is met.

Example with simple error

import {throwIf} from 'rxjs-throw-if';
import {interval} from 'rxjs';

 interval(1000).pipe(
    throwIf(v => v === 5, 'Index should not exceed 4'),
).subscribe(console.log, console.error);
// Prints: 0, 1, 2, 3, 4 and then errors

Example of error based on last value

 interval(1000).pipe(
    throwIf(v => v === 5, errorValue => `Index should not exceed 4, got ${errorValue}`),
).subscribe(console.log, console.error);
// Prints: 0, 1, 2, 3, 4 and then errors with 'Index should not exceed 4, got 5'

The second argument of throwIf is optional and allows you to pass any value which will be thrown when the predicate is met. If the argument is a function, that function will be executed with the value that caused the predicate to be met and its result will be thrown.

If the predicate function throws, that error will be rethrown.

About

RxJS operator which throws an error when a predicate is met

License:MIT License


Languages

Language:Shell 69.4%Language:TypeScript 30.6%