RxJS forkJoin does not complete

一曲冷凌霜 提交于 2019-12-09 20:53:12

问题


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;
    }
  }

This code is not executed when using 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));

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


回答1:


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.

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).

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



来源:https://stackoverflow.com/questions/47450639/rxjs-forkjoin-does-not-complete

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!