Within my Angular controller, I have the following code snippet...
function performTask() {
// Display the loading modal
$scope.modal = $ionicLoading.show({
content: 'Fetching current location...',
showBackdrop: false
});
console.log("get orig pos");
// Retrieve the initial position
PositionService.getPosition(posOptions)
.then(function(positionArr) {
// First location obtained // this part does not run!
console.log("got original pos: " + positionArr.length);
$scope.locationArray = positionArr;
$scope.dataReceived = true;
$scope.modal.hide();
}, function(err) {
// An error occurred
console.log("something wrong in getPos");
$timeout(function() {
$scope.modal.hide();
}, 3000);
});
console.log("get next pos");
PositionService.getPosition(posOptions)
.then(function(positionArr) {
// Second location obtained // this part executes!
console.log("got new pos: " + positionArr.length);
$scope.locationArray = positionArr;
$scope.dataReceived = true;
}, function(err) {
// An error occurred
console.log("something wrong in getPos");
});
}
After running the program, I noticed that the PositionService.getPosition
function is called twice as expected. However, only one of the then
blocks is actually executed. Shouldn't both then
blocks be invoked? Am I misunderstanding how this functions in Javascript? This is what the getPosition
function looks like...
getPosition : function(posOptions) {
return $cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function(position) {
positionArr = positionArr.concat({
position: position,
status: 'new'
});
return positionArr;
}, function(err) {
console.log("PositionService: error getting position");
return err;
});
},
UPDATE:
Here is the requested console output...