Search code examples
javascriptjsonnode.jsstack-overflow

How to perform url requests one-by-one and not to cause stack overflow? (Node.JS)


Currently i have this code

function DownloadPage(uri) {
    request(uri, function (err, res, body) {
        if (err) {
            console.log(err);
        } else {
            nextURL = FindURLBySomeLogic(body);
            DownloadPage(nextURL);
        }
    });
}
DownloadPage("http://example.com/");

but i think that i will get stack overflow after some amount of pages. Amount of pages are infinite. How can I avoid this? Every page contain only JSON data.


Solution

  • console.log(new Error().stack); in DownloadPage will show you that the stack size does not increase because request is async.