Can't get forkJoin to shoot

I am creating a defense in Angular where I need to make two different HTTP requests and based on both determine whether to continue or not. I noticed that forkJoin

is the correct way to do it, but I cannot get it to fire.

In my code, I have:

this.userService.watchCurrentUser().subscribe(data => { console.log(data) });
this.orgService.watchOrg().subscribe(data => { console.log(data) });
Observable.forkJoin(
    this.userService.watchCurrentUser(),
    this.orgService.watchOrg()
).subscribe(data => {
    console.log(data);
});

      

The first two subscriptions were added later to check if there were calls, and they are; I see logs from them. But I never see much of forkJoin

.

I import it on top of my file:

import { Observable, BehaviorSubject } from 'rxjs/Rx';
import 'rxjs/add/observable/forkJoin';

      

Is there something else I'm missing to use forkJoin

?

+3


source to share


2 answers


Observable.forkJoin()

requires all Observables to emit at least one element and terminate.

Are you sure both of your sources are being monitored?



If they don't (and based on their internal logic, they can't), you might prefer to use zip()

or combineLatest()

.

+3


source


In your case, guard, you only need one result for each thread. So you have to do this:

Observable.forkJoin(
    this.userService.watchCurrentUser().first(),
    this.orgService.watchOrg().first()
).map(data => {
    let permitted: boolean;
    // check the data you want to.
    return permitted; 
});

      



In the code above, fork concatenates the first result of the two observables and both results will be matched against a boolean.

0


source







All Articles