【问题标题】:call a function in case of httpClient timeout in Angular在Angular中httpClient超时的情况下调用一个函数
【发布时间】:2018-09-05 14:03:41
【问题描述】:

我有一个函数在服务器上发送几个请求,每个请求都有一个超时:

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

在超时(30s)的情况下,每个人的请求都会被取消,这是可以理解的。 我的问题是,在超时的情况下,取消的请求不会被视为错误,因此它不会进入请求的错误部分。 在这种情况下是否有可能调用函数?

【问题讨论】:

  • 因为无论结果如何,它都会在超时完成后触发。在这种情况下,请尝试根据您的预期检查响应或删除超时

标签: angular timeout httpclient


【解决方案1】:

如果你使用 Angular 6,超时处理的方式是pipe/timeout,请看这个例子:

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

这个片段来自我为演示 HTTP 拦截器而编写的这个示例:https://stackblitz.com/edit/angular-my-http-interceptor?file=src%2Fapp%2Fapp.component.ts

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2013-09-03
    • 2012-11-14
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多