1

I am doing custom $http service that looks something like this:

angular.factory('myHttp', function($http){
    var obj = {};

    obj.get = function(path) {
        return $http.get(path,{timeout: 5000}).error(function (result) {
                console.log("retrying");
                return obj.get(path);
        });
    }
});

The system works fine. It does return the data when success, and retrying when connection fail. However, I am facing problem that it will return to controller when the connection is timeout. How can I prevent it from returning and continue retrying?

8
  • Lookup the retry logic in your code and fix it there. Sorry can't give you any other advice without seeing your code. Retry is NOT a $http feature. Commented Nov 5, 2015 at 10:13
  • @Michael That's my code up there.... To use it, it can be use via myHttp.get($scope.url).success(function(response) {}); Any advice to get it works? Commented Nov 5, 2015 at 10:17
  • @Michael It's there..... return obj.get(path); The logic works and it will retrying even when it is timeout. The problem is when it is timeout, it will not wait for retrying and returning.... Commented Nov 5, 2015 at 10:30
  • Al right.. yeah I was look somewhere else. You should use .then(null, errorCallback) instead of error. Using then you can return a promise, but not with using error. error is deprecated anyway and shouldn't be used anymore. Commented Nov 5, 2015 at 10:48
  • @Michael Thanks a lot for your help. Using your suggestion method, I am able to do retrying. However, another problem appear. That is when using myHttp.get('url').then(successCallback, errorCallback), even if the service return a fail connection, it will return to successCallback not errorCallback. How can I make it to errorCallback? Commented Nov 5, 2015 at 14:05

2 Answers 2

1

You need to use $q.reject. This will indicate that the error handler failed again and the result should populated to the parent error handler - NOT the success handler.

  obj.get = function(path) {
    return $http.get(path, {
      timeout: 5000
    }).then(null, function(result) {
      console.log("retrying");
      if (i < retry) {
        i += 1;
        return obj.get(path);
      } else {
        return $q.reject(result); // <-- use $q.reject
      }
    });
  }

See plunker

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks a lot for your great help.
I face one problem. How can I reset the counter i when the connection is success?
Put the counter i into the get function and wrap the $http request so you can do a retry without reset the counter. With this setup the counter i starts always at 0. No matter if the previous request was successful or not. See updated plunker
Thanks a lot for your reply. But how can I access the updated plunker?
With the plunker link in this answer
|
0

See the reject.status to determine the timeout

  $http.get('/path', { timeout: 5000 })
    .then(function(){

      // Your request served

    },function(reject){


      if(reject.status === 0) {

         // $http timeout

      } else {

         // response error 

      }
    });

Please see the following question for a good overview about handling timeout errors:

Angular $http : setting a promise on the 'timeout' config

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.