Closed gopinathshiva closed 7 years ago
Sure, but you need to add timeout in response interceptor since it will have actual http observable:
httpInterceptorService.response().addInterceptor(res =>
res.timeoutWith(
AppConstants.httpTimeout,
Observable.defer(() => Observable.throw(new Error(AppConstants.httpTimeoutMessage))))
.map(Utils.successHandler)
.catch((err) => Utils.errorHandler(err)));
Appreciate the solution given.
I tried the above solution. Since the response interceptor gets called only after getting http response. So setting timeout over there doesn't help in my case. (Since I need to set timeout to http call initially)
Is there anything I am doing wrongly? My full http interceptor code below,
`httpInterceptor.request().addInterceptor((data, method) => {
this.incrementRequestCount();
return data;
});
httpInterceptor.response().addInterceptor((res, method) => {
res.timeoutWith(
5000,
Observable.defer(() => Observable.throw(new Error("Request Timeout"))))
return res.do((data) => {
this.decrementRequestCount();
}, e => {
console.log(e);
this.decrementRequestCount();
});
});`
Timeouts and delays will work just fine as long as you return modified observable.
In your example you setting timeout on observable and do not return it, instead you only returning .do
observable.
So just merge both operators in one chain and return that final Observable (because each operator returns new observable and does not mutate original one).
Cool. That works merging both operators and returning final observable. Really Appreciate that.
Adding final code for a reference to anyone,
`httpInterceptor.response().addInterceptor((res, method) => {
return res.timeoutWith(
AppConstants.httpTimeout,
Observable.defer(() => {
this.decrementRequestCount();
return Observable.throw(new Error("Request Timeout"))
}
)
).do((data) => {
this.decrementRequestCount();
});
});`
Right now I am adding http timeout individually in all http calls in angular like
this.http.get(baseApiUrl) .timeoutWith(AppConstants.httpTimeout,Observable.defer(() => Observable.throw(new Error(AppConstants.httpTimeoutMessage)))) .map(Utils.successHandler) .catch((err) => { return Utils.errorHandler(err); });
Is there any way I can make of use ng-http-interceptor , in such a way that I can add timeout to all http calls globally in httpInterceptor.request().addInterceptor method.