Let's say you have:
function myPromise() {
// using some promise lib other than $q, like Q or bluebird
var defer = Q.defer();
defer.resolve('John');
return defer.promise;
}
and then you have something like:
function foo() {
return myPromise().then(function (name) {
return name + ' Smith';
});
}
foo().then(function (name) {
$scope.$apply(function () { // or can use $timeout
console.log('after foo');
$scope.text = 'Hey ' + name;
});
});
Is there a way to modify foo so that the after foo block is automatically wrapped in $scope.$apply()? i.e. we can then just have:
foo().then(function (name) {
console.log('after foo');
$scope.text = 'Hey ' + name;
// $scope.$apply() is automatically called after this block is executed
});
The reason I ask is that I am developing an API where the user will make several of these foo calls and it would greatly cut down on the user's code if the digest cycle was automatically triggered after the foo promise resolves. I know I can use a callback to accomplish this, but I'm hoping there is a cleaner way that just uses promises.
My hunch is that the solution lies in wrapping the non-$q promise in a $q promise...