Javascript defer 不返回的递归函数
Javascript recursive function with defer not returning
我得到了这个递归函数。我可以看到它在数据 return 为 null 时循环,但它没有 return 在完成递归任务后数据不为 null 时的承诺。好像完成递归任务后,承诺在某处丢失了。有人会指出我在这里做错了什么吗?
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);
});
你没有在递归的情况下解决延迟
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).done(function(data){
d1.resolve(data);//pass any data required
});
} else {
d1.resolve(null);
}
}
});
return d1.promise();
}
callrq1(globalsystemid, globalgraphid, starttimeobj.start, starttimeobj.end, endtimeobj.start).then(function (data) {
console.log(data);
});
你错过了在递归调用的情况下解决你的延迟。但是,你 shouldn't be using a deferred 首先是为了这个!只需链接一个 then
回调和 return 函数的结果承诺。您甚至可以 return 来自回调的承诺,我们将其用于递归情况:
function callrq1(globalsystemid, globalgraphid, start, end, lastcheck) {
var datetimeformat = "YYYY-MM-DD HH:mm:ss";
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
return requeststring1.then(function(data) {
//^^^^^^ ^^^^
if (data != null) {
return 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) {
return callrq1(globalsystemid, globalgraphid, newstart, newend, theend);
// ^^^^^^
} else {
return null;
// ^^^^^^
}
}
});
}
我得到了这个递归函数。我可以看到它在数据 return 为 null 时循环,但它没有 return 在完成递归任务后数据不为 null 时的承诺。好像完成递归任务后,承诺在某处丢失了。有人会指出我在这里做错了什么吗?
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);
});
你没有在递归的情况下解决延迟
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).done(function(data){
d1.resolve(data);//pass any data required
});
} else {
d1.resolve(null);
}
}
});
return d1.promise();
}
callrq1(globalsystemid, globalgraphid, starttimeobj.start, starttimeobj.end, endtimeobj.start).then(function (data) {
console.log(data);
});
你错过了在递归调用的情况下解决你的延迟。但是,你 shouldn't be using a deferred 首先是为了这个!只需链接一个 then
回调和 return 函数的结果承诺。您甚至可以 return 来自回调的承诺,我们将其用于递归情况:
function callrq1(globalsystemid, globalgraphid, start, end, lastcheck) {
var datetimeformat = "YYYY-MM-DD HH:mm:ss";
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
return requeststring1.then(function(data) {
//^^^^^^ ^^^^
if (data != null) {
return 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) {
return callrq1(globalsystemid, globalgraphid, newstart, newend, theend);
// ^^^^^^
} else {
return null;
// ^^^^^^
}
}
});
}