The issue is in one angular component, I subscribe to two Observable
(i.e. ObsA
and ObsB
) to get data from two BehaviorSubject
, in ObsB.subscribe
, I would like to build new object per each data item from ObsA.subscribe
, this is when the concurrent nature of Observable
gets in the way, basically, ObsB.subscribe
seems to happen earlier than ObsA.subscribe
, so there is always no ObsA
's data to be used to build new objects in ObsB.subscribe
. Below is the code that illustrate this:
In MyComponent.ts
ObsAData: any = null;
ObsBData: any = null;
ngOnInit() {
ObsA.subscribe(dA => {
this.ObsAData = dA;
});
ObsB.subscribe(dB => {
if (!!dB) {
//do work
this.ObsBData = dB;
} else {
//no data from ObsB source, hence create new data here based on data from ObsA source
this.CreateNewBData(this.ObsAData);
}
});}
The problem is at runtime, this.ObsAData
is always null when it gets to the this.CreateNewBData
call. In a sense, creating new ObsBData
depends on the availability of ObsAData
, so my natural instinct is to try to somehow "chain" these subscriptions such that the ObsB.subscribe
happens after ObsA.subscribe
, but i am not even sure that's logical as it totally runs against the concurrent nature of Observable
. Any suggestions how to do this effectively? that is to ensure this.CreatenewBData
always have non-null this.ObsAData
as its argument AFTER this.ObsAData
indeed is set to non-null data from this.ObsA
source.