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

angular2 rxjs方式检查forkJoin完全完成了

发布时间:2020-12-17 17:26:06 所属栏目:安全 来源:网络整理
导读:我想知道是否有一些rxjs操作我可以在forkJoin之后调用以了解我的并行异步任务是否已经完成,所以我可以在angular2模板中使用类似* ngIf =“loaded | async”的东西. 我提出了一个解决方案,但对我来说似乎很麻烦. 我敢打赌,有一种更清洁的rxJS方式. plunkr如果
我想知道是否有一些rxjs操作我可以在forkJoin之后调用以了解我的并行异步任务是否已经完成,所以我可以在angular2模板中使用类似* ngIf =“loaded | async”的东西.

我提出了一个解决方案,但对我来说似乎很麻烦.
我敢打赌,有一种更清洁的rxJS方式.

plunkr如果你需要它:
https://plnkr.co/edit/UFVCJpjKAEGguMls5eIl?p=preview

public loaded:Observable<boolean>;

constructor(private http:Http) {
    // This is the best solution I could come up with. Is there a cleaner way
    // by just calling some operation after forkJoin instead of building my own observable?
    this.loaded = Observable.create((observer) => {
        observer.next(false);
        Observable.forkJoin(
            this.something1(),this.something2(),() => {
                // now we know we have finished
                observer.next(true);
            }
        ).subscribe();
    });
}

something1() {
    return this.http.get('test1.json').map((res) => res.json());
}

something2() {
    return this.http.get('test2.json').map((res) => res.json());
}

更新:
根据Luka的评论,试过这个.效果很好.好多了:

this.loaded = Observable.combineLatest(
    this.something1(),() => {
        // now we know we have finished
        return true
    }
);

解决方法

使用combineLatest.这样的事情应该有效:

this.loaded = this.something1().combineLatest(
    this.something2(),function (s1,s2) { return true }
).take(1);

还要注意,我最后调用take(1),以确保我们只发出一个事件,因为它只会被加载一次.

(编辑:李大同)

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

    推荐文章
      热点阅读