I am a bit confused about this. I have two get calls inside a function. Once this complete function, that is the two get calls are done, only then is this function done with its work. how should I used $q to get this to work as I want it? This is what I have now:
function updateBlackList() {
$http.get("http://127.0.0.1:8000/blacklist/entries/vehicle").then(function (res){
console.log(res)
}).catch(function (err) {
console.log(err)
});
})
$http.get("http://127.0.0.1:8000/blacklist/entries/person").then(function (res){
console.log(res)
}).catch(function (err) {
console.log(err)
});
});
return $q.when();
}
Here withint another function I need to wait for the above fiunction to complete:
BlackListService.updateBlackList().then(function() {
addVersion(server_version).then(function () {
console.log("Blacklist update complete")
})
})
Its not doing it like I was suspecting it to do. The Blacklist complete console is called before the tw get request are done
You want to combine both promises in one with $q.all()
function updateBlackList() {
return $q.all([
$http.get("http://127.0.0.1:8000/blacklist/entries/vehicle")
.then(function (res){console.log(res)})
.catch(function (err) {console.log(err)}),
$http.get("http://127.0.0.1:8000/blacklist/entries/person")
.then(function (res){console.log(res)})
.catch(function (err) {console.log(err)});
]);
}
Also, for your second example, you can chain promises to have a better looking code:
BlackListService.updateBlackList()
.then(function() {
return addVersion(server_version);
})
.then(function () {
console.log("Blacklist update complete");
})