【发布时间】:2015-06-27 03:15:18
【问题描述】:
我尝试了不同的方法来做到这一点,但都失败了。问题是我有一个 http.get 请求查询给定日期的数据(一整天的总数),一旦返回,我在 .then() 内运行多个 http.get 请求以将这一天拼接到用户的班次中假如。问题是说我有 7 天想查询每天 3 个班次。我运行查询,当一天结束时,我将它记录到一个对象中。三个 http 请求同步运行,并且应该推送到嵌套在 key shift 下的那一天。将会发生的事情是前几个工作正常,但是奇数时间它将轮班推到第二天,使第二天的轮班总数为 4 而不是 3。其他时候它使第一天的轮班和第二天有两个.它永远不会超过总班次,在这种情况下为 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;
}
有人可以提供任何帮助,让我的电话等到其他人完成,因为似乎这一天并没有等待轮班电话完成:|
【问题讨论】:
-
使用 $Q - 谷歌它。不是想成为一个混蛋,我只是不够了解,无法简明扼要地解释,但这是为了延迟执行。我真的不明白你为什么要这样做。
-
@VSO 已经在使用 $q 在它说 var thePromise =$q.defer();对于每个班次,我还有另一个用于我为白天数据所做的主要呼叫。不知道你认为我还需要它吗?我也对其他方式持开放态度,只需要这个工作是主要的
标签: javascript angularjs http http-get angularjs-http