检查8秒内是否没有返回响应

Eug*_*ukh 2 javascript rxjs typescript angular

我的 Angular 组件中有返回数据的方法

这是这个方法

 getRecognitionById() {
    this.loaderService.show(null, true);
    forkJoin(
      this.vendorWebApiService.getRecognitionById(this.executiveChangeId),
      this.vendorWebApiService.getLatestFeedback(this.executiveChangeId)).pipe(take(1))
      .subscribe(res => {
        this.recognitionData = res[0];
        this.latestFeedback = res[1];
        this.generateResponseFeedbackGroup();
        this.loaderService.hide(true);
      });
  }
Run Code Online (Sandbox Code Playgroud)

我需要检查 8 秒内是否没有响应,提醒用户alert(response.exceptionMessage);

我怎样才能做到这一点?

eko*_*eko 5

您可以使用timeout运算符:

// import { throwError, TimeoutError } from 'rxjs';
// import { catchError, timeout } from 'rxjs/operators';

getRecognitionById() {
    this.loaderService.show(null, true);
    forkJoin(
      this.vendorWebApiService.getRecognitionById(this.executiveChangeId),
      this.vendorWebApiService.getLatestFeedback(this.executiveChangeId)).pipe(
        take(1),
        timeout(8000),
        catchError((err) => {
          if (err instanceof TimeoutError) {
            // alert(response.exceptionMessage);
          }

          return throwError(err);
        }),
      )
      .subscribe(res => {
        this.recognitionData = res[0];
        this.latestFeedback = res[1];
        this.generateResponseFeedbackGroup();
        this.loaderService.hide(true);
      });
  }
Run Code Online (Sandbox Code Playgroud)