I have this code:
var geocode = function(value) {
var request;
.....
var dResult = Q.defer();
geocoder.geocode(request, function (results) {
dResult.resolve(results);
});
return dResult.promise;
};
var cancelWatch;
$scope.$watch('value', function (value) {
$timeout.cancel(update);
update = $timeout(function () {
$scope.geocodedResult = geocode(value);
}, 300);
});
in line 15 $scope.geocodedResult is a promise that sooner or later will become the result value and the scope should refresh. This unfortunately does not happen. The code works if I do
geocode(value).then(function(result) {
$scope.geocodedResult = result;
$scope.$digest();
});
What am I doing wrong?
UPDATE:
I'm now trying to use only $q but I cannot get it to work:
this.getCurrentPosition = function () {
var dCurrentPosition = $q.defer();
if (currentPosition) {
dCurrentPosition.resolve(currentPosition);
} else {
navigator.geolocation.getCurrentPosition(function (cp) {
currentPosition = cp;
dCurrentPosition.resolve(currentPosition);
});
}
return dCurrentPosition.promise;
};
this.getCurrentLoc = function () {
return self.getCurrentPosition().then(function (currentPosition) {
return [currentPosition.coords.longitude, currentPosition.coords.latitude];
});
};
a breakpoint in
return [currentPosition.coords.longitude, currentPosition.coords.latitude];
will never get triggered while it works fine with Q