Code Monkey home page Code Monkey logo

rxjs-throw-if's Introduction

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.

rxjs-throw-if's People

Contributors

dependabot[bot] avatar matthiaskunnen avatar

Watchers

 avatar  avatar

rxjs-throw-if's Issues

Allow throwing anything

Hi Matthias,

i came across this and it's exactly what I needed, except...restricting the second parameter to a string limits the use of this operator. The built-in RxJS error operators don't have this limitation, so removing it would make this operator more consistent with the rest of RxJS and broaden its application scope.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.