2

I was wondering about the drawbacks of not writing complete statement in code written below. what will happen in this case ?

Observable.create(function(observer) {
  observer.next('Hello');
  observer.next('World');
  // observer.complete(); 
});
Prakash Verma
  • 470
  • 3
  • 11
  • https://stackoverflow.com/questions/44770999/observables-complete-vs-finally-vs-done http://reactivex.io/documentation/observable.html – martin Jun 25 '19 at 10:41

1 Answers1

5

If you don't call .complete(), the subscribers will never be aware that your Observable will not emit events anymore. By calling .complete(), all of the subscribers will unsubscribe and free the allocated memory, thus preventing memory leaks. You can suppose that the subscribers will unsubscribe themselves (based on events content, events count, or whatever else), but it is strongly advised to emit a "completed" event as it will prevent your Observable from being misused.

Also as written in the comments, some operators simply won't work if the Observable does not complete (e.g. concatMap will wait for Observables to complete before switching to the next one)

Guerric P
  • 30,447
  • 6
  • 48
  • 86
  • 2
    Also if you dont complete your own observables you are maybe wondering why e.g concatMap not working as expected – enno.void Jun 25 '19 at 11:58