RXJS Observable doSomething onComplete

As Thierry Templier answered one option is to leverage the finally/finalize (rxjs docs) (but it's not the same as using the complete event, see the notes)


last

Another option to do something on complete is last (rxjs/last)

enter image description here


The subscribe method accepts three callbacks. The last one is for the complete event.

doSome() {
  this.service.myMethod()
      .subscribe((num:any) => {
        console.log(num);
      }, (err) => {
        this.handleError(err);
      }, () => { // <----
        this.handleComplete();
      });
}

You could also leverage the finally operator for this.

doSome() {
  this.service.myMethod()
      .catch(this.handleError)
      .finally(this.handleComplete) // <----
      .subscribe((num:any) => {
        console.log(num);
    });
}

note: there is a difference between the two examples in case we have errors: if we would add console.logs we would see that in the first case only handleError is printed

-> handleError

in the second case

-> handleError
-> finally

in other words finally is always called, were complete is not.