angular – 在observer.error()之后需要调用observer.complete()吗?

前端之家收集整理的这篇文章主要介绍了angular – 在observer.error()之后需要调用observer.complete()吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在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

猜你在找的Angularjs相关文章