RxJS forkJoin未完成 [英] RxJS forkJoin does not complete

查看:254
本文介绍了RxJS forkJoin未完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我订阅getAllSubModules时,forkJoin会无误地执行所有这些可观察的对象,但是不会完成.我知道forkJoin仅在其所有可观察值都完成后才能完成,但作为证明,我在控制台中看到"-----" 3次,确认一切均已成功,因此其所有可观察值都已完成.

When I subscribe to getAllSubModules forkJoin executes all those observables without error but does not complete. I know forkJoin completes only after all its observables completed but as a proof I see '-----' 3 times in console which confirm everything is successful so all its observables completed.

 getSubmodules(id): Observable<any> {
    return this.authService.getToken()
      .flatMap((token) => this.http.get(`${this.URL}/ROS/applications/modules/${id}/subModules?token=${token}`))
      .map((res: any) => res.data.map((subModule) => this.mapSubModules(subModule)));
  }
  getAllSubmodules(): Observable<any> {
    const tasks = [];
    this.modules.forEach((module: AppModule) => {
      const obs = this.getSubmodules(module.id).map((subModules) => {
        this.allSubModules[module.id] = subModules;
        console.log('--------------------');
      });
      tasks.push(obs);
    });
    return Observable.forkJoin(...tasks).retry(2);
  }
  mapSubModules(moduleData) {
    if (moduleData.id) {
      const subModule = <SubModule> {
        id: moduleData.id,
        parentId: moduleData.parentId,
        typeId: moduleData.typeId,
        name: moduleData.name.az,
        active: true
      };
      return subModule;
    }
  }

使用 forkJoin 时不执行此代码:

 this.universityService.getAllSubmodules().subscribe(() => {             
        // --- Below is not executed!--
        console.log('subModules in Report Co');
        console.log(this.universityService.allSubModules);
        this.checkUrl();
        this.showChild = true;
      }, (er) => console.log(er));

但是当我使用 combineLatest 而不是forkJoin时,它会按预期工作. 那是什么问题?希望有人提出建议.

but when I use combineLatest instead of forkJoin it works as expected. So what is problem?Hope someone give advice.

推荐答案

您的期望不正确.发出3次console.log('--------------------')仅表示您已收到3个onNext事件. forkJoin等待所有可观察对象完成.

Your expectation is incorrect. Having console.log('--------------------') emitted 3 times only means that you have received 3 onNext events. forkJoin waits for all observables to complete.

尝试使用.do(next=>{},err=>{},complete => console.log('completed'))查看单个流或使用.take(1)和/或.timeout(1000)明确定义流何时完成的情况.

Try what happens if you look at the individual streams with .do(next=>{},err=>{},complete => console.log('completed')) or explicitly define when your streams should complete using .take(1) and/or .timeout(1000).

authService..getToken()发出一个值后完成吗?

Does authService..getToken() complete after emitting one value?

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆