我的代码类似于下面的代码。它应该在中间打印"然后"然后"在最后,然后",但它打印"在最后然后"首先(即在调用deferred.resolve()之前调用then函数)。知道为什么会发生这种情况吗?它与$ timeout有关吗?
var promise = myFunctionThatReturnsAPromise();
promise.then(function() {
console.log('in final then!');
});
var myFunctionThatReturnsAPromise = function() {
var deferred = $q.defer();
$timeout(function() {
console.log('in intermediate then');
deferred.resolve();
}, 0);
return deferred.$promise;
};