angular promise callback not getting called
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)
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.
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.
any reason you're not using $timeout? It handles this stuff for you
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.
fixed in 1.2.0+