rintoj / statex

StateX is a state management library for modern web applications with unidirectional data flow and immutable uni-state (just like redux)
MIT License
68 stars 18 forks source link

How do you use an Observable's error handling? #9

Closed Nxt3 closed 6 years ago

Nxt3 commented 6 years ago

@rintoj

I have something like this:

  @action()
  public searchBatches(state: AppState, payload: SearchBatchesAction): Observable<AppState> {
    return Observable.create((observer: Observer<AppState>) => {
      observer.next({ hasError: false, isLoading: true });

      this.eventListService.fetchEvents(payload.batchId).subscribe(
        (e: Array<EventLogModel>) => {
          observer.next({
            batchId: payload.batchId,
            events: e || [],
            isLoading: false
          });
        },
        error => {
          console.log('errored out');
          observer.next({
            events: [],
            isLoading: false,
            hasError: true
          });
        }
      );
      observer.complete();
    });
  }

but if there is an error, it never goes inside the error block. How am I supposed to do this?

Nxt3 commented 6 years ago

The problem I was having was calling observer.complete() outside of the subscription. I got this working now.