Calling observer.complete() is required after observer.error()?

Viewed 5822

I am writing an observable in Angular 2. My code is something like:

Observable.create(observer => {
    // fetched something from web service
    if (some condition) {
        observer.next('something');
        observer.complete();
    }
    else { // error with no data
        observer.error('something else');
        observer.complete(); // Is this required here? Or can I skip this in case of error() ?
    }
});
1 Answers

You don't need to call complete, it won't do anything anyway because you called error already.

Doc says: http://reactivex.io/documentation/observable.html

By the terms of the Observable contract, it may call onNext zero or more times, and then may follow those calls with a call to either onCompleted or onError but not both, which will be its last call

You can have a look at the source code:

Related