Looking to add offline support to my angular web-app, but struggling to get my simple retry function to function properly. My service code is as follows:
constructor($http: ng.IHttpService, private $q: ng.IQService, private $interval: ng.IIntervalService, private $timeout: ng.ITimeoutService) {
this.httpService = $http;
}
getCategories(updatedFunction: Function) {
var deferred = this.$q.defer();
this.httpService.get('/api/listCategories')
.success((response) => {
deferred.resolve(response);
}).error(() => {
this.$timeout(() => this.getCategories(), 10000);
});
return deferred.promise;
}
On the controller side, I have the following:
service.getCategories().then(c => {
//Code here
});
When online, the service works fine and I receive the expected data. However, if I am offline at the start, I start receiving "GET http://correct_looking_path/ ERR_FAILED" and when I debug the JS file, I see that the httpService.get method runs but instantly fails to .error(). Even after connecting, I still receive the same error message and pattern. The httpService.get method runs every 10 seconds but fails consistently.
Refreshing the page seems to solve the issue.