I have tried different ways to do this but they all have failed. The issue is I have an http.get request that queries data for a given day (total for the entire day), once returned I run multiple http.get requests inside the .then() to splice the day into the shifts the user has provided. The problem is that say I have seven days I want to query with 3 shifts per day. I run the query and when the day is done I log it into an object. The three http requests run synchronously and should push onto that day nested under the key shifts. What will happen is the first few work alright but the odd time it pushes a shift into the next day making the shift totals for the next day 4 instead of 3. Other times it makes the shifts for the day 1 and the next day has two. It never exceeds what the total shifts should be, in this instance 21 but balances itself throughout the days. Any idea on how to approach this. currently what I am doing looks something like this
Main Function for the Day:
function nextDay(startDate, endDate, processObj) {
//console.log('I am in the next()');
d = startDate;
if (d <= endDate) {
//console.log();
da = new Date(d);
da.setDate(da.getDate() + 1);
$http.get(WEB CALL HERE).then(function(response) {
// Store the username, get the profile.
dataAggregator(response.data, d, da);
}).then(function() {
if ($scope.reporting.shiftsEnabled === true) {
var tempPromise = $q.defer();
var tempCntr = 0;
nextShift(tempPromise, tempCntr, startDate, endDate, processObj);
} else {
d.setDate(d.getDate() + 1);
nextDay(d, endDate, processObj);
}
}, function(error) {
console.log('Failure...', error);
});
nextShift Call Being Made:
function nextShift(shiftPromise, cntr, startDate, endDate, processObj) {
var d = startDate;
if (cntr < $scope.shiftsObj.length) {
var weekday = new Array(7);
weekday[0] = "sunday";
weekday[1] = "monday";
weekday[2] = "tuesday";
weekday[3] = "wednesday";
weekday[4] = "thursday";
weekday[5] = "friday";
weekday[6] = "saturday";
tempStartDate = new Date($scope.shiftsObj[cntr].startTime);
tempStartDate = new Date(d.getFullYear(), d.getMonth(), d.getDate(), tempStartDate.getHours(), tempStartDate.getMinutes(), 0, 0);
tempEndDate = new Date($scope.shiftsObj[cntr].endTime);
if ($scope.shiftsObj[cntr].endTime < $scope.shiftsObj[cntr].startTime) {
tempEndDate = new Date(da.getFullYear(), da.getMonth(), da.getDate(), tempEndDate.getHours(), tempEndDate.getMinutes(), 0, 0);
} else {
tempEndDate = new Date(d.getFullYear(), d.getMonth(), d.getDate(), tempEndDate.getHours(), tempEndDate.getMinutes(), 0, 0);
}
var webShiftPromise = $q.defer();
var webShiftCallReturn = webShiftCall($scope.reporting.machineSelect.SoftwareKey, $scope.reporting.machineSelect.Address,
processObj, tempStartDate, tempEndDate,
$scope.reporting.hardware, webShiftPromise, cntr);
webShiftCallReturn.then(function(retData) {
var thePromise = $q.defer();
shiftDataAggregator(retData, tempStartDate, tempEndDate, shiftPromise, cntr);
}, function(error) {
console.log('Failure...', error);
});
cntr++;
nextShift(shiftPromise, cntr, startDate, endDate, processObj);
} else {
d.setDate(d.getDate() + 1);
shiftPromise.resolve(nextDay(d, endDate, processObj));
}
return shiftPromise.promise;
}
Any help that someone can give to make my calls wait until the others complete as it seems that the day does not wait for the shifts calls to complete :|
I ended up fixing my own problem by re-writing the functions to run one after the other instead of inside each other...