【发布时间】:2015-09-19 09:55:15
【问题描述】:
我得到了这个递归函数。我可以看到它在数据返回为空时循环,但在完成递归任务后数据不为空时它没有返回承诺。好像完成递归任务后,承诺在某处丢失了。有人会指出我在这里做错了什么吗?
var callrq1 = function(globalsystemid, globalgraphid, start, end, lastcheck) {
var datetimeformat = "YYYY-MM-DD HH:mm:ss";
var d1 = new $.Deferred();
var request1 = "../system/" + globalsystemid + "/highcharts.xml?type=" + globalgraphid + "&start=" + start + "&end=" + end;
var requeststring1 = makejson(request1); //this makejson function is an ajax get and return promise
requeststring1.done(function(data) {
if (data != null) {
d1.resolve(data);
} else {
var theend = moment(lastcheck).format(datetimeformat);
var newstart = moment(end).format(datetimeformat);
var newend = moment(end).add(1, 'weeks').format(datetimeformat);
if (newend <= theend) {
//recursive callrq1
callrq1(globalsystemid, globalgraphid, newstart, newend, theend);
} else {
d1.resolve(null);
}
}
});
return d1.promise();
}
callrq1(globalsystemid, globalgraphid, starttimeobj.start, starttimeobj.end, endtimeobj.start).then(function(data) {
console.log(data);
});
【问题讨论】:
标签: javascript ajax recursion promise deferred