0

I am trying to implement a simple timeout functionality to my promise. Goal is if I do not receive response say in 1 second, then the request should be cancelled i.e. code should not wait for the response neither should the code after success be called. This seemed to me very simple code but I don't know why its not working. Following is my code:

var canceler = $q.defer();
var timeoutPromise = $timeout(function() {
    canceler.resolve(); //abort the request when timed out
    console.log("Timed out");
    }, 1000);
$http.put(PutUrl, PurDataObject, {timeout: canceler.promise})
  .then(function(response){
        // control should never come here if the response took longer than 1 second
});

Any help is appreciated. I am using AngularJS v1.5.5.

1 Answer 1

1

No need to use $q.defer() as the $timeout service already returns a promise:

var timeoutPromise = $timeout(function() {
    console.log("Timed out");
    return "Timed out";
}, 1000);

$http.put(PutUrl, PurDataObject, {timeout: timeoutPromise})
  .then(function(response){
        // control should never come here if the response took longer than 1 second
});
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.