简体   繁体   中英

Observable subscription - Ignore next method

I don't want to do anything on next() method, I want to handle error() and complete() .

This is my current solution that works well:

this.myService.myServiceObservable(params)
  .subscribe(
    () => {
      /**/
    },
    error => {
      // Handling errors here
    },
    () => {
      // Handling `complete()` here
    }
  );

I feel that this: () => { /**/ } is not the most elegant solution.

Anybody knows how to have the same result but not this ugly? Am I missing something?

You can use undefined instead of a notification handler:

.subscribe(undefined,
  err => { ... },
  () => { ...}
);

Or you can pass so called "PartialObserver" object that has only handlers for the notifications you want:

.subscribe({
  error: err => { ... },
  complete: () => { ...}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM