I am trying to make nested calls using rxjs with nested forkjoins. However, I am facing an issue where the outer forkJoin stops returning a result when the inner observable has a flatMap inside it.
Here's the code to illustrate.
// Only returns a value when the flatMap statements are removed from the inner observables it is waiting for
searchAdmin(searchKey: string): Observable<[StudentAccountSearchResult[], InstructorAccountSearchResult[]]> {
return forkJoin(
this.getStudentAccountSearchResults(searchKey),
this.getInstructorAccountSearchResults(searchKey)
);
}
private getStudentAccountSearchResults(searchKey: string): Observable<StudentAccountSearchResult[]> {
return this.getStudents(searchKey).pipe(
map((students: Students) => students.students),
// Outer forkjoin in searchAdmin returns when I remove the line below
flatMap((studentsArray: Student[]) => forkJoin(studentsArray.map(student => this.createStudentAccountSearchResult(student)))),
);
}
private getInstructorAccountSearchResults(searchKey: string): Observable<InstructorAccountSearchResult[]> {
return this.getInstructors(searchKey).pipe(
map((instructors: Instructors) => instructors.instructors),
// Outer forkjoin in searchAdmin returns when I remove the line below
flatMap((instructorsArray: Instructor[]) => forkJoin(instructorsArray.map(instructor => this.createInstructorAccountSearchResult(instructor))))
)
}
Both getStudentAccountSearchResults
and getInstructorAccountSearchResults
return the proper value when I subscribe to them directly, so there's no error while mapping the observables.
Does anyone know why the flatMap in innerObservables causes the outer forkJoin to stop returning any results? Thanks!
I'm on rxjs 6.4
forkJoin()
will emit only when all its inner observables emit at least once and complete so that's probably the problem with one of your observables. – martin