Reputation: 470
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();
});
Upvotes: 2
Views: 603
Reputation: 31815
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)
Upvotes: 5