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?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
user1995781
  • 19,085
  • 45
  • 135
  • 236
  • 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. – Michael Nov 05 '15 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? – user1995781 Nov 05 '15 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.... – user1995781 Nov 05 '15 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. – Michael Nov 05 '15 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`? – user1995781 Nov 05 '15 at 14:05
  • Please show the code you have now. – Michael Nov 05 '15 at 14:22
  • @Michael Here is the link http://plnkr.co/edit/fx8wL4JvzJR9ZrkoJemD?p=preview – user1995781 Nov 05 '15 at 14:50
  • @Michael Do you have any idea to make it works? – user1995781 Nov 06 '15 at 06:09

2 Answers2

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

Michael
  • 3,085
  • 1
  • 17
  • 15
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

Community
  • 1
  • 1
Charlie
  • 22,886
  • 11
  • 59
  • 90