Rapiernik
Rapiernik

Reputation: 111

Is there a chance to omit 'next' part from RxJs 'subscribe'

I'm wondering if I can transform following piece of code to one without 'next' part from RxJS 'subscribe'. I'm not interested here to get any results from service. The only thing, which matters here is catching potential error to react properly.

    this.someService.getSomething(this.id)
      .pipe(
          takeUntil(this.destroy$)
      )
      .subscribe(() => {
        // I don't need this part at all
      }, (error: HttpErrorResponse) => {
        if (error.status === 404) {
           // doing sth important
        }
      });

Is there a chance to do something with that in Angular?

Upvotes: 2

Views: 671

Answers (3)

frido
frido

Reputation: 14109

You can pass a PartialObserver to subscribe and define or omit next, error and complete:

source.subscribe({ error: (e: any) => console.error(e) })

Upvotes: 2

cuddlemeister
cuddlemeister

Reputation: 1785

As answered above, you can use catchError operator. But, if you need to subscribe anyway, you may also use the subscribe method like this this.someService.getSomething(this.id).subscribe(null, (error) => {..})

Upvotes: 1

alphapilgrim
alphapilgrim

Reputation: 3975

You can try something with catch / catchError:

DEMO

this.someService.getSomething(this.id)
  .pipe(
    takeUntil(this.destroy$),
    catchError(
      (error: HttpErrorResponse) => {
        if (error.status === 404) {
          // doing sth important
        }
      })
  );

Upvotes: 3

Related Questions