With the following code I'm intending to subscribe to function1
, and then depending on the result (saleAgreed), subscribe to a second observable; either function2
or of(undefined)
. See below:
this.apiService.function1(id).pipe(
switchMap(async (saleAgreed: boolean) => {
// Wait for some input from the user
let cancelAgreedSale = await this.promptSaleAgreedCancelDetails();
// These are both evaluating to 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);
}
It's my understanding that my code should evaluate the result of the anonymous function I provide to iif()
. If it evaluates to true, function2 should be subscribed to, otherwise it should subscribe to of(undefined)
. function1
is subscribed to fine, however even though my anonymous function is evaluating to true, function2
is not being subscribed to. What am I doing wrong? Thanks!