0

I'm writing a web scraper, which looks like

function get_links(url){
    var links = new Array();
    get_page(url,function(page){
        links = parse_links(page);
    });
    return links;
}

while(true){
    var a = queue.pop_front();
    queue.push.apply(queue, get_links(a));
}

(the actual code contains other unrelated details, so I simplify it)

The problem is that, sometimes return is executed before the callback function, when links is still empty.

sqd
  • 1,485
  • 13
  • 23
  • 1
    ... It *always* will be. You're going to need to wrap your head around async programming; you can't blindly "return values" from async functions. Use promises, use callbacks, use something that's async-oriented instead of pretending async isn't how all this stuff works. – Dave Newton Aug 18 '15 at 03:34
  • 1
    http://jsfiddle.net/arunpjohny/vqyncLr3/1/ – Arun P Johny Aug 18 '15 at 03:35

0 Answers0