Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thenSource of if() still running even though condition is false #2768

Closed
WhatsThatItsPat opened this issue Jul 25, 2017 · 4 comments
Closed

Comments

@WhatsThatItsPat
Copy link
Contributor

RxJS version:
5.4.2

Code to reproduce:

I have an issue where Observable.if() isn't protecting me against a null value like I expect it would.

If I have a variable, whatever, that can be null or a simple object...

let whatever = null;
// or
whatever = {something: 'abc123'};

...and if I use Observable.if() like this...

Rx.Observable.if(
  () => !!whatever,
  Rx.Observable.of(`true: ${whatever.something}`),
  Rx.Observable.of('unprotected condition false')
).subscribe(next => {
  console.log(`unprotected:`, next);
});

...when whatever is null, I get an error: Cannot read property 'something' of null.

Shouldn't the false condition stop the thenSource from running?

If I "wrap" the thenSource stream in a switchMap, it works; I'm protected from the null value and the elseSource runs.

Rx.Observable.if(
  () => !!whatever,
  Rx.Observable.of('discard')
    .switchMap(discard => Rx.Observable.of(`true: ${whatever.something}`)),
  Rx.Observable.of('condition false')
).subscribe(next => {
  console.log(`protected:`, next);
});

Additional information:

Here's a codepen to play with:
https://codepen.io/patrickmcd/pen/BZeeQK?editors=0010

@paulpdaniels
Copy link
Contributor

paulpdaniels commented Jul 25, 2017

The of isn't running but you are setting up the initial value immediately by doing

`true: ${whatever.something}`

That isn't an RxJS issue it is a javascript one. Personally I have always wished that if was passed function that created Observables rather than just being fed raw Observables. But if you want to work around it you can do as you did or you can do:

Observable.if(
  () => !!whatever,
  Observable.defer(() => Observable.of(`true: ${whatever.something}`))
)

@WhatsThatItsPat
Copy link
Contributor Author

Got it. At least I'm not going crazy.

Put another way, I think, is that the word "if" has fooled me into expecting it to work as an if/else statement, and caused me to forget that if() is just a function.

Thanks for the defer tip. That looks way less hacky than my solution.

And congrats on the book release btw. I'm looking forward to another read when I get my dead tree version.

@paulpdaniels
Copy link
Contributor

No problem and thanks!

@lock
Copy link

lock bot commented Jun 6, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants