0

I got this recursive function. I can see it loop through when data return is null but it did not return the promise when data is not null after done the recursive task. Seem like when finish doing the recursive task, the promise is lost somewhere. Would anyone point out what did I do 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 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);
});
mido
  • 24,198
  • 15
  • 92
  • 117
Phuong Le
  • 377
  • 5
  • 16

2 Answers2

0

You are not resolving the deferred in the case of recursion

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);
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

You missed resolving your deferred in the case of the recursive call. However, you shouldn't be using a deferred for this in the first place! Just chain a then callback and return the result promise from your function. You can even return promises from the callback, which we use for the 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 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;
//      ^^^^^^
      }
    }
  });
}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I also tried this way. Return the result promise from makejson works. Yes I'll bite this as one less defer/promise eventhough the code look quite advance to follow through, what do you think the advantage from this compare to the above? Thanks very much for the input. – Phuong Le Jul 02 '15 at 23:58
  • It's more concise, more functional, faster, less errorprone, easier to understand (more natural flow)… Please check out the link in my answer. – Bergi Jul 03 '15 at 00:03
  • Thanks Bergi, yes just digesting your link, one more thing to learn :) . I'm from C background and so used to code implicitly. Thanks very much! – Phuong Le Jul 03 '15 at 00:14