Ok guys, I know there are a lot of posts on promises but I cannot seem to figure out how to apply them to my situation. I am NOT using $http, I am using google places api. I have a login page. User logs in successfully and is taken to a listview page that is populated with data from google places. I know I need to use a promise but I do not know how to set it up. I have a function that will get the data and I have a function that will use the data to populate the array. The problem is obviously the are running out of order. Can someone please help me.
it depends how the service works in most cases you can define a callback function
You have to build the correct success and error handling
function myApiCall() {
var deferred = $q.defer();
GoogleApi.doSomething(function (data) {
if (data.err) {
deferred.reject(data.err);
} else {
deferred.resolve(data);
}
});
return deferred.promise;
}
myApiCall().then(function (data) {
// success case
}, function (err) {
// error
});
Thanks but I think this may be more than what I need to do. I will try to explain my intention as best I can.
- User is on login screen
- A successful login will call google api
- This will in turn populate the empty array that is in a factory
- Then the user is to be directed to the list page via a $state.go()
5 The list page controller has a function that gets the data from the factory
What I need to do it to what until the data fetch has been completed successfully and THEN rout the user to the list page.
The problem is that the list page is blank because the data is not there yet.
for those stuff loading-spinners and overlays are invented^^
It is not good to let the user wait until he know, that he is correctly logged in --> redirect if the user is logged in -> show a loading overlay ($ionicLoading) or a spinner (ionSpinner) until the data is there.
Ok, can you point me to a working example of a spinner that is making a web service call and waiting until that call returns before moving the the next page??
i faked a apicall with $timeout.
the function myAsyncStuff could a service-function are another promise function as well.