加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:23:46 所属栏目:安全 来源:网络整理
导读:我在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.erro
我在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读