简体   繁体   English

等待递归函数完成

[英]Waiting for Recursive Function To Complete

I have a recursive Javascript function which gets the links from one Wikipedia page, follows them, and then gets all of those links (repeating a specified number of times). 我有一个递归Javascript函数,该函数从一个Wikipedia页面获取链接,跟随它们,然后获取所有这些链接(重复指定的次数)。

It calls itself an unknown number of times to construct an object of a known depth. 它称自己为未知次数,以构造一个已知深度的对象。 When it completes, I want to output the object. 完成后,我要输出对象。 Currently the object immediately outputs, and is empty, meaning the function obviously isn't waiting for all the recursive calls to complete. 当前,该对象立即输出,并且为空,这意味着该函数显然不等待所有递归调用完成。

As you can see, I have attempted to use callbacks, but I assume incorrectly. 如您所见,我尝试使用回调,但是我做错了。 What am I doing wrong, how should I be doing it? 我在做什么错,应该怎么做? I'm going to presume there's a few other things wrong I haven't spotted too; 我将假定还有其他一些我没有发现的错误。 I'm relatively new to Javascript. 我对Java比较陌生。

$(document).ready(function ()
{
  pageLinks[START_PAGE] = {};
  //Get initial pages
  links = getLinks(START_PAGE, 0, printLinks));
});

function printLinks()
{
  console.log(links);
}

function getLinks(currentPage, level, callback)
{
  visitedPages.push(currentPage)
  var pageLinks = {}
  var data = $.getJSON(URL_BEGIN + currentPage + URL_END, function(data)
  {
    var pages = data.query.pages;
    for(var page in pages)
    {
      pageContentObj = pages[page].revisions[0];
      for(var key in pageContentObj) if(pageContentObj[key].length > 100)
      {
        var pageContent = pageContentObj[key];
        //Get links
        hyperlinks = getFromBetween.get(pageContent,"[[","]]");
        for(var link in hyperlinks)
        {
          link = hyperlinks[link].split("|")[0]; //Remove friendly name
          link = link.replaceAll(" ", "%20");

          //Add to pagelist object
          prefix = link.split(":")[0];
          if(prefix != "Category" && prefix != "File" && prefix != "wikipedia")
            if(level < ITERATIONS && !visitedPages.includes(arguments, link))
            {
              console.log(level + ": " + link)
              pageLinks[link] = getLinks(link, level+1, callback); //===Recursive call===
            }
        }
      }
    }
  });
  if(level == 0 && callback) callback();
  return pageLinks;
}

Any help is appreciated, thanks in advance. 任何帮助表示赞赏,在此先感谢。

**EDIT: ** Link: https://github.com/JakeStanger/Wikipedia-Mapper/blob/master/init.js#L53 **编辑:**链接: https//github.com/JakeStanger/Wikipedia-Mapper/blob/master/init.js#L53

The recursive call needs to be like this: 递归调用需要像这样:

var counter = 0;
//the big for loop
counter++;
getLinks(link, level + 1, function(res) {
    for (var key in res) { //with an array it would be concat...
        pageLinks[key] = res[key];
    }
    counter--;
    if (counter == 0 && callback) callback(pageLinks); //callback if all callbacks called
});

Also remove this weird code: 同时删除此奇怪的代码:

if(level == 0 && callback) callback();

No you can do: 不,你可以做:

getLinks(START_PAGE, 0, console.log);

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

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