Having an issue with a recursive function. It appears to loop through effectively when the data return is null, but it fails to return the promise correctly when the data is not null after completing the recursive task. It seems like the promise gets lost somewhere upon finishing the recursive process. Can anyone help me identify what I might have done wrong here?
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 method performs ajax get and returns a 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);
});