[英]$http.get request with multiple $http.get requests inside it not running properly
我尝试了不同的方法来执行此操作,但是它们都失败了。 问题是我有一个http.get请求,可以查询给定日期(整天的总数)的数据,一旦返回,我就会在.then()内部运行多个http.get请求,以将这一天与用户的班次相结合提供。 问题是说我有7天要每天查询3个班次。 我运行查询,并在一天结束后将其登录到对象中。 这三个http请求同步运行,并应推迟到按键移位嵌套的那一天。 将会发生的事情是前几项工作正常,但是将其推迟到第二天的奇数时间使第二天的总移位变为4,而不是3。其他时候,第一天和第二天的移位为2。 。 它永远不会超过总班次,在这种情况下为21,但在整天内保持平衡。 关于如何解决这个问题的任何想法。 目前我在做什么看起来像这样
当天的主要功能:
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正在拨打电话:
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;
}
某人可以给我打电话的任何帮助会等到其他人完成为止,因为那天似乎没有等待轮班电话完成:|
我最终通过重新编写要一个接一个地运行而不是彼此内部运行的函数来解决自己的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.