call a function in case of httpClient timeout in Angular

后端 未结 1 1333
暗喜
暗喜 2021-01-25 06:08

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

this.httpClient.get(url, { headers: headers })
        .timeout(30000)
        .         


        
相关标签:
1条回答
  • 2021-01-25 06:42

    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

    0 讨论(0)
提交回复
热议问题