Javascript递归函数,延迟不返回

时间:2015-07-02 03:00:58

标签: javascript ajax recursion promise deferred

我有这个递归功能。我可以看到它在数据返回为null时循环,但是在完成递归任务后数据不为null时它没有返回promise。似乎在完成递归任务时,承诺在某处丢失。有谁会指出我在这里做错了什么?

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);
});

2 个答案:

答案 0 :(得分:0)

在递归的情况下,您没有解决延迟

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);
});

答案 1 :(得分:0)

在递归通话的情况下,您错过了解决延期的问题。但是,你首先要shouldn't be using a deferred!只需链接then回调并从函数返回结果承诺。您甚至可以从回调中返回promise,我们将其用于递归案例:

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