Vugar Abdullayev
Vugar Abdullayev

Reputation: 2075

RxJS forkJoin does not complete

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.

Upvotes: 7

Views: 6652

Answers (1)

Mark van Straten
Mark van Straten

Reputation: 9425

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?

Upvotes: 6

Related Questions