10

In rxjs what exactly does the observer.complete() do after observer.next() ?

Karan Hudia
  • 553
  • 3
  • 19
  • 3
    See http://reactivex.io/documentation/observable.html, I'm sure you can find similar questions like this https://stackoverflow.com/questions/34097158/how-can-i-complete-observable-in-rxjs – martin Jan 16 '18 at 10:21
  • What the documentation didn't tell me is that if it unsubscribes the observable as well or not ? – Karan Hudia Jan 16 '18 at 11:17

2 Answers2

10

From the documentation observer.complete notifies the Observer that the Observable has finished sending push-based notifications.

In the other hand, observer.complete it's a callback function and an Observable calls this method after it has called next() for the final time, if it has not encountered any errors.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • 6
    So basically it automatically unsubscribes from the observable ? – Karan Hudia Jan 16 '18 at 11:16
  • Yah I was wondering the same thing. Is that equivalent to automatically unsubscribing? – mtpultz May 10 '18 at 23:38
  • 4
    I had the same question & tested it out, it seems like the observers stop receiving notifications once calling observer.complete(), which makes me think calling complete() effectively is unsubscribing the observers. – mpkasp Feb 10 '20 at 21:16
5

In ReactiveX library, there are two types of messages.

First ones are ordinary messages. Ordinary messages are the ones sent with .next() and there can be 0-many of them.

Second type are notifications. These can be of two types - error and success. The error is sent with .error() and give some error details in it (like exception) and success is sent with .complete() meaning that there will intentionally be no messages. Every observable should end with a single error or a single success notification.

eddyP23
  • 6,420
  • 7
  • 49
  • 87