'rxjs: any "joining" alternative to withLatestFrom operator?
observable1.pipe(
withLatestFrom(observable2),
.. do something with both emitted observable values
)
Problem with withLatestFrom is that if observable2 hasn't emitted any event before the observable1 did then it's a dead code. How should I modify the code to ensure that do something with both emitted observable values will have have both observable values emitted at least once prior to the call? Maybe some wrapper around the forkJoin?
Solution 1:[1]
You could use the RxJS combineLatestWith if you're looking for a pipable operator instead of combineLatest function.
observable1.pipe(
combineLatestWith(observable2),
// ... do something with both emitted observable values
)
Solution 2:[2]
Looks like a use case for the combineLatest operator. This operator takes an array of observables and waits for every to emit at least once. Once all the observables have emitted, the operator emits for the first time. After that it emits every time any of the observables emits.
Solution 3:[3]
You can enforce a first emission with startWith(), you apply it to both observable 1 and 2 depends on your need.
observable1.pipe(
withLatestFrom(observable2.startWith(null)),
.. do something with both emitted observable values
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ruth |
| Solution 2 | I.Orlovsky |
| Solution 3 | Fan Cheung |
