RxJS forkjoin停止订阅是否正在等待的其中一个可观察对象中有一个flatMap

问题描述 投票:0回答:2

我正在尝试使用带有嵌套forkjoins的rxjs进行嵌套调用。但是,我遇到一个问题,当内部可观察对象内部包含flatMap时,外部forkJoin停止返回结果。

这里是说明代码。

  // 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))))
    )
  }

getStudentAccountSearchResultsgetInstructorAccountSearchResults都在我直接订阅它们时返回正确的值,因此在映射可观察对象时没有错误。有谁知道为什么innerObservables中的flatMap导致外部forkJoin停止返回任何结果?谢谢!

我正在使用rxjs 6.4

angular rxjs observable rxjs6
2个回答
0
投票

我想说内部forkJoin可能是不必要的,因为已经有外部forkJoin。目前,生成的代码可能类似于forkJoin(forkJoin(...), forkJoin(...))。取而代之的是,我们可以将所有可观察对象直接传递到外部forkJoin。尝试以下]]

private getStudentAccountSearchResults(searchKey: string): Observable<StudentAccountSearchResult[]> {
  return this.getStudents(searchKey).pipe(
    map((students: Students) => students.students.map(student => this.createStudentAccountSearchResult(student))),
  );
}

private getInstructorAccountSearchResults(searchKey: string): Observable<InstructorAccountSearchResult[]> {
  return this.getInstructors(searchKey).pipe(
    map((instructors: Instructors) => instructors.instructors.map(instructor => this.createInstructorAccountSearchResult(instructor))),
  )
}

0
投票

答案是@martin在原始帖子的评论中发布的。

© www.soinside.com 2019 - 2024. All rights reserved.