My current code involves a mix of nested Promises and subscriptions, but all I want to do is this:
- Call my function bar() and determine if it executed successfully or encountered an error
Current approach: Currently, my bar() function returns a boolean Observable. I handle errors separately for Promises using .catch and for rxjs Observables with the error handler:
foo(): void {
this.bar.subscribe((data: boolean) => console.log('the function call bar() was ' + data));
}
bar(): Observable<boolean> {
let subject = new Subject<boolean>();
this.httpHandler.newFoo().subscribe(
(data) =>
this.checkBar(data)
.then((barData) =>
this.httpHandler.updateBar().subscribe(
(barData2) => subject.next(true),
(error) => subject.next(false)))
.catch(err => subject.next(false)),
(error) => subject.next(false));
return subject.asObservable();
}
This setup may seem convoluted and excessive for simply determining if the bar() function execution went smoothly. Is there a cleaner way to manage nested errors in this scenario?
Thank you for your help