Hi have 2 observables ,
- this.resolveActifsFromStore() // return Observable
- this.store.select(fromActifSelector.DoSelector.selectAllActifs) // return Observable<IActifModel[]>
I need to execute by order, so I use the concatWith like this
actifs$!: Observable<IActifModel[]>;
ngOnInit(){
this.resolveActifsFromStore()
.pipe(
concatWith(this.store.select(fromActifSelector.DoSelector.selectAllActifs)),
)
.subscribe((val)=>{
//error first return is boolean , the second is IActifModel[]
//HERE IS I DON´T KNOW HOW TO DO
if(val) {
actifs$ = of(val);
}
}
The problem is the val will have the return boolean, and also IActifModel[]. How can I identify the type of each return, because I want to do this, if the first observables is true then I want to assign the value of the second observable to actifs$
CodePudding user response:
You can use Array.isArray
. It inferes the type correctly and knows within the if block, that the type of val is an array.(https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)
.subscribe((val: boolean | IActifModel[])=>{
if(Array.isArray(val)) {
// code for IActifModel[]
} else {
// code for boolean
}
CodePudding user response:
You could use concatMap
to wait for the first observable to complete and then proceed to the second one:
this.resolveActifsFromStore().pipe(
withLatestFrom(this.store.select(fromActifSelector.DoSelector.selectAllActifs),
concatMap(([myBoolean, myIActifModel]: [boolean, IActifModel[]]) => {
//....
return of({
theBoolean: myBoolean,
theOtherOne: myIActifModel
});
})
).subscribe(console.log);