As someone who is new to the concept of promises and how they work, I have been trying to understand how to create and implement them in my code. Although I have read documentation on the topic and grasp the idea behind a promise, I am struggling with actually using them in my code.
Here is a simplified version of my code:
function start_it_all(){
return $cordovaSQLite.execute(db, "SELECT foo FROM bar").then(function(result) {
return anotherFunction();
});
}
function anotherFunction(){
return $http.get('foo.php').then(function(){
// Here I get some data
});
}
Both the $cordovaSQLite
and the $http
functions are asynchronous. My goal is to be able to do this:
start_it_all().then(function(){
// Hide Loading animation (no further return data needed).
});
I understand that the .then()
function is used as a promise handler. However, as it currently stands, my code only returns the promise of the query from $cordovaSQLite
. I want the promise of $http
to be returned as well. This is where I encounter the error:
TypeError: Cannot read property 'then' of undefined
when calling start_it_all().then()
.
If anyone could provide guidance on how to achieve this, I would greatly appreciate it.