Looking for a solution in AngularJS, I have a service that calls the backend to get some data. Here is how the service looks:
app.factory('myService', ['$http', '$window', '$rootScope', function ($http, $window, $rootScope) {
return {
GetTypeName(typeID, callback) {
$http.get(backend_url + 'type/getById/' + typeID)
.success(function (response, status, headers, config) {
if (status === 200) {
callback(true, response);
} else {
callback(false);
}
})
.error(function (data, status, headers, config) {
callback(false);
});
}
} }]);
Now, I am trying to call this service from a controller like so:
getFormationsType = function(){
for (i = 0; i != array.length; i++){
myService.GetTypeName(typeID, function (result, data) {
if(result){
console.log(data.name);
// do something with data.name here, and also need it outside of this func
} else{
console.log("error");
}
});
}}
The issue I am facing is that the service does not wait for the result before moving on to the next lines of code. It jumps out of the getFormationsType() function and continues executing other tasks without waiting for the result. I need the result immediately after calling the service. How can I make the service wait for the result before continuing?
Your assistance is greatly appreciated.