我在Angular 2中编写了一个observable.我的代码是这样的:
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() ? } });
解决方法
你不需要调用complete,因为你已经调用了错误,它不会做任何事情.
Doc说: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
你可以看一下源代码:
> https://github.com/ReactiveX/rxjs/blob/master/src/Subscriber.ts#L120
> https://github.com/ReactiveX/rxjs/blob/master/src/Subscriber.ts#L108