I have the code below:
data$ = getResponse1$.pipe(
tap((res1: any) => {
this.qService.data = res1;
}),
switchMap(() =>
of(getResponse2$(res2)).pipe(
map((val) => {
const res: any = {
val,
};
return res;
})
)
)
);
Is it possible to run getResponse1$ i.e. the first observable, along with getResponse2$ only when a condition is true? Otherwise, I want to execute only getResponse2$.
CodePudding user response:
Take a look at: Conditional concat in RXJS?
Adopting it to your case:
getResponse2$.pipe(
switchMap(response => [your_condition] ? of(response).pipe(withLatestFrom(getResponse1$)) : of(response))
);
You will get an array as output if condition is true, where the first element of array corresponds to getResponse2$
result value, and second one to getResponse2$
. Otherwise you will get just getResponse2$
result value.