Below is the code I'm using to subscribe to function1
, and based on the result (saleAgreed), I then want to subscribe to a second observable - either function2
or of(undefined)
. Check it out:
this.apiService.function1(id).pipe(
switchMap(async (saleAgreed: boolean) => {
// Waiting for user input
let cancelAgreedSale = await this.promptSaleAgreedCancelDetails();
// Both evaluating as true
console.log(`sale agreed: ${saleAgreed}`)
console.log(`cancelSaleAgreed: ${cancelAgreedSale}`)
iif(
function() {
if (saleAgreed && cancelAgreedSale) {
this.removeSaleAgreedRequest = {
contactId : this.cid,
details : this.cancelSaleAgreedDetails
};
}
return saleAgreed && cancelAgreedSale;
}, this.apiService.function2(this.removeSaleAgreedRequest), of(undefined));
// Thought returning the observable might help, but doesn't make a difference whether
// it's here or not
return this.apiService.function2(this.removeSaleAgreedRequest)
})
).subscribe(res => {
// Returns either undefined or an observable if I return one above
console.log(res)
}, error => {
console.log(error.error)
})
function2
implementation:
public function2(request: RemoveSaleAgreedRequest): Observable<any> {
console.log('api func hit!')
return this.http.patch<any>(this.apiUrl + 'some/endpoint', request);
}
I believe that my code should assess the outcome of the anonymous function passed to iif()
. If it evaluates to true, then function2
should be subscribed to, otherwise it should subscribe to of(undefined)
. While function1
is successfully subscribed to, even though my anonymous function evaluates to true, function2
is not being subscribed to. Can you point out where I'm going wrong? Thank you!