简体   繁体   English

需要有关JavaScript递归和异步方法的建议

[英]Need suggestions on JavaScript recursion and async methods

I have a recursive method that has, contained within, a async method: 我有一个包含在异步方法中的递归方法:

var loadWebsRecursively = function (url) {

    loadWebsDeferred(url).then(function (webs) {

        var wE = webs.getEnumerator();
        webCount += webs.get_count();
        while (wE.moveNext()) {
            var site = wE.get_current();
            var title = site.get_title().toLowerCase();
            var fqdn = siteRootUrl + site.get_serverRelativeUrl() + '/';
            .... // additional work
        }

         webProcessed++;
         loadWebsRecursively(fqdn);
    }
};

I need some suggestions on how I can determine that all recursive calls have finished. 我需要一些有关如何确定所有递归调用已完成的建议。 Each Web could have "N" number of sub-webs. 每个站点可以具有“ N”个子站点。

Thanks in advance! 提前致谢!

It seems that you are looking to resolve a promise sequence. 您似乎正在寻找解决诺言序列的方法。 If you're using javascript's native ES6 promise implementation, then something like the following might be what you're looking for: 如果您使用的是JavaScript的本机ES6 Promise实现,那么您可能正在寻找类似以下内容的东西:

// Loop through our chapter urls
story.chapterUrls.reduce(function(sequence, chapterUrl) {
  // Add these actions to the end of the sequence
  return sequence.then(function() {
    return getJSON(chapterUrl);
  }).then(function(chapter) {
    addHtmlToPage(chapter.html);
  });
}, Promise.resolve());

See here for more details: 请参阅此处以获取更多详细信息:
http://www.html5rocks.com/en/tutorials/es6/promises/#toc-parallelism-sequencing http://www.html5rocks.com/en/tutorials/es6/promises/#toc-parallelism-sequencing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM