Home > Net >  Check if response not returned in 8 seconds
Check if response not returned in 8 seconds

Time:10-12

I have method in my Angular component, that returning data

Here is this method

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

I need to check if responce not came up in 8 seconds, alert user like alert(response.exceptionMessage);

How I can do this?

CodePudding user response:

You could use the timeout operator:

// 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);
      });
  }
  • Related