Tengo un método en mi componente Angular, que devuelve datos.
Aquí está este método
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); }); } Necesito verificar si la respuesta no apareció en 8 segundos, alertar al usuario como alert(response.exceptionMessage);
¿Como puedo hacer esto?
Podrías usar el operador de timeout de espera:
// 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); }); }