0

I have a function sending a few requests on the server with a timeout each like:

this.httpClient.get(url, { headers: headers })
        .timeout(30000)
        .subscribe(
            (response) => {
                ...
            },
            error => {
                ...
            }
                ...
            );

In case of timeout (30s), everyone request gets canceled, which is understandable. My problem is, that in case of a timeout, the canceled request is not seen as an error, so it doesn't go to the error-part of the request. Are there any possibilities to call a function in this case?

Rohit Sharma
  • 3,304
  • 2
  • 19
  • 34
Pandaboon
  • 19
  • 1
  • 1
  • 3
  • because it triggers after the timeout finishes regardless the outcome. in this case try to check the response based on what you expected or remove timeout – Jalal Sep 05 '18 at 14:12

1 Answers1

8

If you are using Angular 6, the way to go with the timeout handling is pipe/timeout, please look at this example:

this.http.get('https://httpstat.us/200?sleep=5000')
  .pipe(timeout(1000))
  .subscribe(response => {
    console.log('forceTimeout', response);
  }, (error) => {
    console.log('Error', error);
    this.error = error.constructor.name;
  })

This fragment is from this example I wrote to demonstrate HTTP Interceptors: https://stackblitz.com/edit/angular-my-http-interceptor?file=src%2Fapp%2Fapp.component.ts

hamilton.lima
  • 1,902
  • 18
  • 29