如何用rxjs实现async await的功能?

<mat-autocomplete
      autoActiveFirstOption
      #auto="matAutocomplete"
    >
      <mat-option
        *ngFor="let option of filteredOptions | async"
        [value]="option.id"
      >
        {{option.title}}
      </mat-option>
    </mat-autocomplete>
  createdFiltered() {
    const lessonNameControl = this.lessonInfo.get("seriesName");
    this.filteredOptions = lessonNameControl.valueChanges.pipe(
      startWith(""),
      map(value => {
        // searchSeries 是一个rxjs请求,这个请求会返回一个数组,但是由于异步原因,并无法获取到这个数组
        return this.searchSeries(value);
      })
    );
  }

在这个函数中,我希望this.filteredOptions是一个数组,问题如代码中描述的样子

阅读 3.9k
2 个回答

把Observerable转换为Promise就可以用async。但你这个地方没必要用async。

你这里不能用map,应该用flatMap。searchSeries返回一个Observerable来异步获取数值。

rxjs里面有个方法,toPromise()可以实现promise的转换

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题