I have to make multiple API calls but have to check the results of each API and then execute the next API call, after reading up on concatmap, doesn't seem like the best option to me, neither does mergeMap. Do I just add the next API to the result of the first API then?
my useCase:
firstAPICall.subscribe(result => {
//execute code
//if success execute 2nd API call
secondAPIcall();
}, error=> {
//error logic
}
}
secondAPIcall() {
service.getAPI2().subscribe(result => {
//second call success logic
}, error=> {
//error logic
}
}
Sounds like you need switchMap with an iif, hit Run code snippet and you have a 50/50 chance of an error.
const { of, throwError, switchMap, iif } = rxjs;
const firstAPICall = () => of({ isSucessful: Math.random() > .5 });
const getAPI2 = () => of('result 2');
firstAPICall().pipe(
switchMap(result => iif(() => result.isSucessful, // Your success condition here
getAPI2(),
throwError(() => 'I make a booboo')
))
).subscribe({
next: (result) => {
console.log(result);
},
error: (error) => {
console.log(error);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.0/rxjs.umd.min.js"></script>