I am facing a dilemma with two observables that I need to combine and use in subscribe, where I want the flexibility to either use both arguments or only one. I have experimented with .ForkJoin, .merge, .concat but haven't been able to achieve the desired behavior.
For instance:
obs1: Observable<int>;
obs2: Observable<Boolean>;
save(): Observable<any> {
return obs1.concat(obs2);
}
When utilizing this function:
service.save().subscribe((first, second) => {
console.log(first); // int e.g. 1000
console.log(second); // Boolean, e.g. true
});
or
service.save().subscribe((first) => {
console.log(first); // int e.g. 1000
});
Is there a way to achieve this specific behavior?
I hope someone can provide assistance!
EDIT:
In my particular situation obs1<int>
and obs2<bool>
represent two distinct post requests: obs1<int>
is the actual save operation and obs2<bool>
checks if another service is active.
The value of obs1<int>
is necessary for refreshing the page after the request is completed, while the value of obs2<bool>
is required for displaying a message if the service is running - regardless of obs1<int>
.
If obs2<bool>
emits before obs1<int>
, it's not an issue as the message will be displayed before the reload. However, if obs1<int>
emits first, the page will reload and the message may not appear.
I mention this because the provided answers yield different outcomes based on whether the values are emitted before or after completion of the other observable, impacting the overall scenario.