Before making a second service call, I need to retrieve a zipcode. My initial plan was to obtain the zipcode before calling the 2nd service by wrapping it in a promise, so that I could access the promise later. However, chaining promises in this way is proving to be difficult.
Angular factory is invoked and within the factory method:
var userGeoPromise = userService.getGeoposition().then(function (geoposition) {
vm.geoposition = geoposition;
return addressService.reverseGeocode(geoposition.coords);
}).then(function (data) {
vm.currentLocation = googleService.googleAddressComponentsToAddress(data.results[0]);
zipCodeCurrent = vm.currentLocation.zip;
});
A couple of points to note from the code above:
- I stored the promise in
var userGeoPromise
zipCodeCurrent
is populated with the obtained zipcode
Testing the Promise works as expected:
userGeoPromise.then( function() {
console.log('should always display', zipCodeCurrent);
});
2nd service call:
userGeoPromise.then( function() {
var serviceBase = "http://localhost:2295/api/getservicezip/"+ zipCodeCurrent;
var serviceZipPromise = $http.get(serviceBase);
return serviceZipPromise.then(function (results) {
console.log('serviceZipPromise', results);
return results.data;
});
});
However, upon including the serviceZipPromise.then
within the other promise, the website modal simply keeps spinning without progress.