0

As documented in the angular docs,

timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved.

Right now I am setting timeout to promise, so I can manually cancel the request by promise.resolve().

Right now, I also want to make it capable to config timeout value, instead of having the request timeout being 120 seconds.

How can I configure it without affecting the existing cancel request functionality?

1 Answer 1

3

You could do somthing like this

$scope.qPromiseCall = function()
{
       var timeoutPromise = $timeout(function()
       {       
               //aborts the request when timed out
               canceler.resolve(); 
               console.log("Timed out");
        }, 250); 

//we set a timeout for 250ms and store the promise in order to be cancelled later if the data does not arrive within 250ms

     var canceler = $q.defer();
     $http.get("data.js", {timeout: canceler.promise} )
     .success(function(data)
     {
           console.log(data);
           $timeout.cancel(timeoutPromise);
           //cancel the timer when we get a response within 250ms
    });

  }

For more details look at

Setting a timeout handler on a promise in angularjs

First Answer by @Khanh TO

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

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.