Currently, I am attempting to iterate through an object and fetch data for each iteration from two separate service API functions. These functions return promises due to using the $http object, which means I must wait for the responses before populating my object.
This is my first time working with the $q AngularJS library and I'm uncertain if I'm using it correctly. It appears that I am not as expected results are not being returned, causing issues with my code.
Below is the snippet of my controller function:
$scope.returnTasksAndPayments = function() {
var defer = $q.defer;
var promisesTasks = [];
var promisesPayments = [];
for(var i in $scope.projects) {
promisesTasks[i] = ProjectService.fetchTaskData($scope.projects[i].project_id);
promisesPayments[i] = ProjectService.fetchPaymentsData($scope.projects[i].project_id);
}
$q.all(promisesTasks, promisesPayments).then(function(promiseArray) {
console.log(promiseArray);
});
};
Here are the related service functions:
this.fetchProjectsData = function(options) {
return api().get("http://dashboards.blurgroup.dev/src/projectPayments/services/JSONdata.json", {
order: options.order,
page: options.page,
page_size: options.page_size,
status: options.status,
search: options.search,
assigned: options.assigned
});
};
this.fetchTaskData = function(options) {
return api().get("http://dashboards.blurgroup.dev/src/projectPayments/services/JSONdataTasks.json", {
});
};
this.fetchPaymentsData = function(options) {
return api().get("http://dashboards.blurgroup.dev/src/projectPayments/services/JSONdataPayments.json", {
});
};
In essence, I am looping through $scope.projects, invoking the two functions for each iteration, storing them in separate promise objects, and then utilizing $q.all to ensure both promises are fulfilled.
The issue lies in the fact that the data stored in the 'promisesPayments' array within the $q.all function isn't being added to the promiseArray variable in the $q.all callback. If anyone can provide insight into where I may be making a mistake, I would greatly appreciate it. Thank you.