rxjs-throw-if
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

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.

Package Sidebar

Install

npm i rxjs-throw-if

Weekly Downloads

14

Version

3.0.0

License

MIT

Unpacked Size

7.92 kB

Total Files

13

Last publish

Collaborators

  • -waylander-