Observable.forkJoin() doesn't execute
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
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([]);
Just add observer.complete();
Will not work:
observer.next(...)
Will work:
observer.next(...);
observer.complete();
Hope it helps.