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

iif subscriptions #7488

Closed
borosbela opened this issue Jul 9, 2024 · 1 comment
Closed

iif subscriptions #7488

borosbela opened this issue Jul 9, 2024 · 1 comment

Comments

@borosbela
Copy link

borosbela commented Jul 9, 2024

Describe the bug

The documentation says, that only trueResult of the given observables will be subscribed, but both of the falseResult and trueResult will be subsrcibed to, and only returns the trueResult observable's value.

Expected behavior

Only subsrcibe to the trueResult observable.

Reproduction code

iif(() => false,
 firstObs(),
 secondObs()
)

function firstObs(): Observable<string> {
 console.log("Why?"); // <--- this got logged
 return of("one way");
}

function secondObs(): Observable<string> {
 console.log("Why not?"); // <--- this got logged
 return of("other way");
}

Reproduction URL

No response

Version

7.8.1

Environment

No response

Additional context

No response

@jakovljevic-mladen
Copy link
Member

Hi @borosbela,

If you subscribe to your iif Observable, you'll see that it only logs other way. one way is not logged and will not be logged as long as the function passed as the first parameter to iif returns falsy value. Please visit this link for demo.

The reason why Why? gets logged is because you're actually calling firstObs function when preparing the second parameter for iif which calls console.log("Why?") before returning of("one way"). This has nothing to do with RxJS, it's just how JavaScript works.

If you need to postpone execution of firstObs, you can use this:

import { defer, iif } from 'rxjs';

iif(() => false,
 defer(firstObs),
 defer(secondObs)
)

I'm gonna close this issue as there's no bug here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants