ajax - Javascript recursive function with defer not returning -


i got recursive function. can see loop through when data return null did not return promise when data not null after done recursive task. seem when finish doing recursive task, promise lost somewhere. point out did 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 function ajax , 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); }); 

you missed resolving deferred in case of recursive call. however, shouldn't using deferred in first place! chain then callback , return result promise function. can return promises callback, use recursive case:

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 ajax , 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; //      ^^^^^^       }     }   }); } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -