For quite some time, I've been relying on the promise generated by a simple http method. However, my current need involves chaining multiple API calls, processing the data, and then returning that processed data. The issue I'm encountering with my existing setup is that the factory isn't returning the promise, causing it to not wait for the data before moving on to the next line in the controller.
app.factory('mytestService', ['$http', function($http){
getSomeDataById: function(id){
var userAllProjects = [];
var myHelperService = this.getSecondData;
this.getFirstData(id).then(function(response){ // first api call
var idObjectValue = response['result'][0]['id'];
myHelperService(idObjectValue).then(function(response){ //second api call
userAllProjects= response['projectList']
});
});
return userAllProjects
}
]);
Now, when attempting to access in the controller:
$scope.allProjects = mytestService.getSomeDataById(1234);
console.log($scope.allProjects);
the console displays undefined. It's clear that this happens because it doesn't wait for the service to complete before moving on to the next line.
I am new to Angular promises and would appreciate any guidance on how to address this issue. Feel free to ask if you require more details.