我有一个函数在服务器上发送一些超时请求,每个请求如下:
this.httpClient.get(url, { headers: headers })
.timeout(30000)
.subscribe(
(response) => {
...
},
error => {
...
}
...
);
如果超时(30秒),每个请求都会被取消,这是可以理解的。
我的问题是,在超时的情况下,取消的请求不会被视为错误,因此它不会进入请求的错误部分。
在这种情况下是否有可能调用函数?
如果您使用 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 https://stackblitz.com/edit/angular-my-http-interceptor?file=src%2Fapp%2Fapp.component.ts
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)