【问题标题】:Javascript recursive function with defer not returning延迟不返回的Javascript递归函数
【发布时间】:2015-09-19 09:55:15
【问题描述】:

我得到了这个递归函数。我可以看到它在数据返回为空时循环,但在完成递归任务后数据不为空时它没有返回承诺。好像完成递归任务后,承诺在某处丢失了。有人会指出我在这里做错了什么吗?

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

【问题讨论】:

    标签: javascript ajax recursion promise deferred


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

    在递归调用的情况下,您错过了解决您的延迟。但是,首先是您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;
    //      ^^^^^^
          }
        }
      });
    }
    

    【讨论】:

    • 我也试过这种方式。从 makejson 工程返回结果承诺。是的,尽管代码看起来很先进,但我会认为这是一个少一点的延迟/承诺,你认为与上述相比有什么优势?非常感谢您的意见。
    • 它更简洁、更实用、更快、更不容易出错、更容易理解(更自然的流程)……请查看我的答案中的链接。
    • 谢谢Bergi,是的,只是在消化你的链接,还有一件事要学习:)。我来自 C 背景,因此习惯于隐式编码。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2012-05-29
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2016-03-01
    相关资源
    最近更新 更多