angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

angular promise callback not getting called

Open chintan-solanki opened this issue 13 years ago • 5 comments

There seems to be a race condition when "then" method on the promise object is called from inside the setTimeout function. It should always work as per this link - http://stackoverflow.com/questions/17852273/returning-angular-promise-from-a-function-that-resolves-the-promise-very-quickly

jsfiddle - http://fiddle.jshell.net/ZjUg3/46/

to reproduce the issue, increase timeout to 10ms (at line 15)

chintan-solanki avatar Aug 26 '13 06:08 chintan-solanki

Adding a $apply() to the setTimeout seems to trigger the alert at any delay. My guess is that when it is 0 it is being fired on the same Digest run, anything higher than 10 or so will cause it to wait for another digest cycle.

setTimeout(function(){

    mypromise.then(function(data){        
        alert(data);        
    });
    $scope.$apply();

},100);

Disclaimer still very new at angular, hope this helps understand the issue a bit more.

Unprofound avatar Aug 30 '13 12:08 Unprofound

Thank you. You are right about the $apply(). The callback does get called irrespective of the delay in setTimeout once i use $apply .

I also observed that it's not just with zero. As long as delay1 >= delay2, it never fires the callback (without the $apply call).

I am not sure if this is by design. But i find it a bit weird that it's promisee' resposibility to ensure that digest cycle is initiated.

chintan-solanki avatar Aug 30 '13 13:08 chintan-solanki

any reason you're not using $timeout? It handles this stuff for you

Burgov avatar Aug 30 '13 13:08 Burgov

Thanks Burgov.

Actually I am writing a utility API that other application code will call. I do not have control over the way promise object will be used. I observed this when i was testing various scenarios of using promise object.

chintan-solanki avatar Aug 30 '13 13:08 chintan-solanki

fixed in 1.2.0+

IgorMinar avatar Jan 05 '14 07:01 IgorMinar