Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix($q): promises should still resolve outside of the $digest/$apply phase #3539

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/ng/q.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,29 @@
*/
function $QProvider() {

this.$get = ['$rootScope', '$exceptionHandler', function($rootScope, $exceptionHandler) {
return qFactory(function(callback) {
$rootScope.$evalAsync(callback);
this.$get = ['$rootScope', '$exceptionHandler','$browser', function($rootScope, $exceptionHandler,$browser) {
return qFactory(function (callback) {
if($rootScope.$$phase){
$rootScope.$evalAsync(callback);
}
else {
var timeoutId = $browser.defer(function(){
timeoutId = false;
$rootScope.$apply();
});
$rootScope.$evalAsync(function(){
if(timeoutId){
//$digest was called before
$browser.defer.cancel(timeoutId);
timeoutId = null;
}
$rootScope.$eval(callback);
});
}
}, $exceptionHandler);
}];
}


/**
* Constructs a promise manager.
*
Expand Down
7 changes: 5 additions & 2 deletions src/ng/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@


function $TimeoutProvider() {
this.$get = ['$rootScope', '$browser', '$q', '$exceptionHandler',
function($rootScope, $browser, $q, $exceptionHandler) {
this.$get = ['$rootScope', '$browser', '$exceptionHandler',
function($rootScope, $browser, $exceptionHandler) {
var deferreds = {};

var $q = qFactory(function(callback){
$rootScope.$evalAsync(callback);
},$exceptionHandler);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$timeout still needs the old nextTick implementation for ngAnimate/etc unit tests to pass.

I am not sure that my hack of $timeout is the appropriate one. Perhaps the promise API should be extended to include another function skipApply() that will suppress the $apply call upon promise resolution/rejection. i.e.:

var defer = $q.defer().skipApply();
// and/or
var promise = somePromiseReturningFunc();
promise.then(onResolve,onErr).skipApply();

even in the skipApply case I would think continuation of the promise chain should not wait for a $digest cycle, but simply prevent dirty checking of the scope.


/**
* @ngdoc function
Expand Down