rxjs-throw-if
v3.0.0
Published
Throw an error when a predicate is met
Downloads
15
Maintainers
Readme
| 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.