Observable forkJoin 不触发

编程入门 行业动态 更新时间:2024-10-27 06:26:45
本文介绍了Observable forkJoin 不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在两个 Observable 上使用 forkJoin.其中一个以流的形式开始...如果我直接订阅它们,我会收到回复,但 forkJoin 不会触发.有什么想法吗?

I'm trying to use forkJoin on two Observables. One of them starts as a stream... If I subscribe to them directly I get a response, forkJoin isn't firing though. Any ideas?

private data$: Observable<any[]>;
private statuses$: Observable<any[]>;
private queryStream = new Subject<string>();    

....

this.data$ = this.queryStream
    .startWith('')
     .flatMap(queryInput => {
            this.query = queryInput
            return this._companyService.getCompanies(this.queryRequired + ' ' + this.query, this.page, this.sort);
                })
            .share();
    
...

Observable.forkJoin(this.statuses$, thispanies$)
            .subscribe(res => {
                console.log('forkjoin');
                this._countStatus(res[0], res[1]);
            });


// This shows arrays in the console...

this.statuses$.subscribe(res => console.log(res));
thispanies$.subscribe(res => console.log(res));

// In the console
Array[9]
Array[6]

推荐答案

forkJoin 的一个很常见的问题是它要求所有源 Observables 至少发出一项,并且所有这些都必须完成.

A very common problem with forkJoin is that it requires all source Observables to emit at least one item and all of them have to complete.

换句话说,如果 this.statuses$thispanies$ 不发出任何项目,直到它们都完成 forkJoin不会发出任何东西.

In other words if this.statuses$ or thispanies$ doesn't emit any item and until they both complete the forkJoin won't emit anything.

this.statuses$.subscribe(
    res => console.log(res),
    undefined,
    () => console.log('completed'),
);

这篇关于Observable forkJoin 不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-27 22:50:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/709852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Observable   forkJoin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!