Skip to content

Commit adeeb52

Browse files
committed
fix($q): promises should still resolve outside of the $digest/$apply phase
Add $rootScope.$apply() call upon resolve/reject if outside a digest phase Closes angular#2431
1 parent 2430347 commit adeeb52

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/ng/q.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,29 @@
161161
*/
162162
function $QProvider() {
163163

164-
this.$get = ['$rootScope', '$exceptionHandler', function($rootScope, $exceptionHandler) {
165-
return qFactory(function(callback) {
166-
$rootScope.$evalAsync(callback);
164+
this.$get = ['$rootScope', '$exceptionHandler','$browser', function($rootScope, $exceptionHandler,$browser) {
165+
return qFactory(function (callback) {
166+
if($rootScope.$$phase){
167+
$rootScope.$evalAsync(callback);
168+
}
169+
else {
170+
var timeoutId = $browser.defer(function(){
171+
timeoutId = false;
172+
$rootScope.$apply();
173+
});
174+
$rootScope.$evalAsync(function(){
175+
if(timeoutId){
176+
//$digest was called before
177+
$browser.defer.cancel(timeoutId);
178+
timeoutId = null;
179+
}
180+
$rootScope.$eval(callback);
181+
});
182+
}
167183
}, $exceptionHandler);
168184
}];
169185
}
170186

171-
172187
/**
173188
* Constructs a promise manager.
174189
*

src/ng/timeout.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33

44
function $TimeoutProvider() {
5-
this.$get = ['$rootScope', '$browser', '$q', '$exceptionHandler',
6-
function($rootScope, $browser, $q, $exceptionHandler) {
5+
this.$get = ['$rootScope', '$browser', '$exceptionHandler',
6+
function($rootScope, $browser, $exceptionHandler) {
77
var deferreds = {};
88

9+
var $q = qFactory(function(callback){
10+
$rootScope.$evalAsync(callback);
11+
},$exceptionHandler);
912

1013
/**
1114
* @ngdoc function

0 commit comments

Comments
 (0)