0

I'm trying to get the resolved promise object after aborting a REST call (using {timeout: canceller.promise}), but I'm unable to get the resolved promise object.

factory:

angular.module('RestModule').factory('abc', function ($http, $rootScope) {
    var canceller = $q.defer();
    $rootScope.$on('CANCEL_REQUESTS', function () {
        canceller.resolve({'isAborted': true});
    });
    return {
        getDetails: function() {
            return $http.get('/test/testREST', {timeout: canceller.promise}).then(function (data) {
                return httpData.data;
            });
        }
});

controller:

.controller('testCtrl', function (abc, $rootScope) {
    abc.getDetails().then(function (data) {
        // success call back
    }, function (error) {
        console.log(error.isAborted);
    });
    $rootScope.$emit('CANCEL_REQUESTS');
});

Here in the error call back I'm not getting the timeout promise object (isAborted: true). error.isAborted is undefined.

2
  • shouldn't you call canceller.reject instead of canceller.resolve, if you're expecting that result in the error handler function? Commented Aug 28, 2014 at 7:05
  • I did that initially but it never came to the error handler in that case. Only if I resolve control is reaching the error handler. Also using canceller.resolve still reaches the error handler Commented Aug 28, 2014 at 7:13

2 Answers 2

1

There are 2 promises in your snippet:

var P1 = var canceller = $q.defer();
var P2 = $http.get(...)..

If you resolve P1 with the value V1, you should expect P1 then callbacks to be called with V1, but not P2 ones.

You should handle cancellation code via the canceller promise (P1):

canceller.promise.then(function(data){
    console.log(data.isAborted);
});

I guess you are expecting Angular to wire up the handler of the timeout resolve promise with the handler of the $http.get reject promise, which I don't think is specified.

Also you might want to reset canceller somewhere in your code, otherwise if you call getDetails() and cancel it , cancelled will be resolved, so the next time your call getDetails(), cancelled will still be in the resolved state, and if you try to cancel it, nothing is likely to happen, as promises are resolved only once.

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

Comments

0

My response gets a config.timeout.$$state object that stores the resolve parameter in value.

.controller('testCtrl', function (abc, $rootScope) {
    abc.getDetails().then(function (data) {
        // success call back
    }, function (error) {
        console.log(error.config.timeout.$$state.value.isAborted);
    });
    $rootScope.$emit('CANCEL_REQUESTS');
});

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.