Observable.forkJoin() doesn't execute

后端 未结 5 1844
萌比男神i
萌比男神i 2020-12-30 23:42

I have the following code:

//Loop: For each user ID/Role ID, get the data
userMeta.forEach((businessRole) => {
  Observable.forkJoin(
    af.database.obje         


        
相关标签:
5条回答
  • 2020-12-31 00:19

    Just add observer.complete();

    Will not work:

    observer.next(...)
    

    Will work:

    observer.next(...);
    observer.complete();
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-31 00:37

    I had the same issue and I could not really get the forkJoin operator work, so I just used the combineLatest, which has worked!

    0 讨论(0)
  • 2020-12-31 00:41

    I've faced a similar issue: I was creating a list of observables dynamically and I've noticed that forkjoin() never emits nor completes if the list of observables is empty whereas Promise.all() resolves with an empty list :

    Observable.forkJoin([])
        .subscribe(() => console.log('do something here')); // This is never called
    

    The workaround I've found is to check the length of the list and to not use this operator when it is empty.

    return jobList.length ? Observable.forkJoin(jobList) : Observable.of([]);
    
    0 讨论(0)
  • 2020-12-31 00:43

    I had a similar issue using Angular 2 / Angularfire 2, specifically where I was looking up whether users existed by email. In one case, the user exists and I received an array of one object from the Observable. In the other case, the user did not exist, and I received an empty array.

    When I used forkJoin with a resultSelector and a subscribe, neither the resultSelector nor the subscribe function ever ran. However, when I tried

    Observable.zip(
      FirebaseListObservable,
      FirebaseListObservable,
      (...results) => {
        return results.map(some code here)
      }
    ).subscribe(res => console.log(res));
    

    Both the selector and the subscribe worked. I assume this has to do with @martin's answer, where forkJoin requires the observables to complete, because by definition it returns the the last emissions. If an observable never completes, I suppose it can never have a last emission.

    Perhaps angularfire list observables (or object observables in your case) never complete, making the use of forkJoin impossible. Fortunately zip has similar behavior and still works, the difference being that it can repeat several times if the data changes in Firebase, where as forkJoin only combines the last response.

    In my case, I'm looking at either 1) using zip and accepting that my code might run multiple times if user data changes while the .zip is still running, 2) manually disable the zip after the first set of data returns, or 3) ditch Angularfire and try out the Firebase api directly, using something like .once to see if I can get an observable that completes and triggers forkJoin.

    0 讨论(0)
  • 2020-12-31 00:45

    forkJoin() requires all source Observables to emit at least once and to complete.

    This following demo completes as expected:

    const source = forkJoin(
      from([1,2,3]),
      from([9,8,7,6])
    ).subscribe(
      x => console.log('GOT:', x),
      err => console.log('Error:', err),
      () => console.log('Completed')
    );
    

    Live demo: https://stackblitz.com/edit/rxjs-urhkni

    GOT: 3,6
    Completed
    

    Jan 2019: Updated for RxJS 6

    0 讨论(0)
提交回复
热议问题