深度嵌套的异步请求未执行。需要帮助修复或调试建议

Deeply nested async request is not executing. Need help fixing or with debugging suggestions

我正在 node.js 中编写缓存预热脚本。它同时使用 Async 和 Request npm 模块。示例代码中唯一真正的变化是变量名称和值。

var languages = ['es', 'en'];
var baseUrl = 'http://localhost:8080/';
var presents = [1,2,3,4];
var foods = ['green eggs', 'roast beast', 'potatas'];

async.each(languages, function (lang, callback) {

    async.each(whos, function (who, callback2) {

        request.get(baseUrl + lang + '/' + who, function (err, res, body) {
            var grinches = body;

            async.each(grinches, function (grinch, callback3) {

                    async.each(presents, function (present, callback4) {

                        async.each(foods, function (food, callback5) {
                            var finalUrl = baseUrl + lang + '/comp/' + who + '/' + grinch + '/' + present + '/' + food;

                            console.log(finalUrl);
                            request.get(baseUrl + 'en/ac', function (error, response, body) {
                                console.log(error);
                                console.log(response);
                                console.log(body);
                                callback5();
                            });
                        }, function () {
                            callback4();
                        });

                    }, function () {
                        callback3();
                    });

                });
            }, function () {
                callback2();
            });

        });
    }, function () {
        callback();
    });

}, function () {
    cb(null, 7);
});

在此示例中,finalUrl 成功记录,但紧随其后的请求中的任何内容都不会记录。如果我抓取其中一个 finalUrl 并将其放入浏览器中,请求将得到正确处理。异步或请求中是否存在我可能遇到的某种堆栈限制?

如果我不是在那个点发出请求,而是将 url 推入一个数组,然后遍历它以从最终的 async.each 回调中发出请求,它们将起作用。但是,此解决方案存在性能(内存)问题,因为我必须维护大量快速增长的数组。这是该解决方案的示例

var languages = ['es', 'en'];
var baseUrl = 'http://localhost:8080/';
var presents = [1,2,3,4];
var foods = ['green eggs', 'roast beast', 'potatas'];
var urls = [];

async.each(languages, function (lang, callback) {

    async.each(whos, function (who, callback2) {

        request.get(baseUrl + lang + '/' + who, function (err, res, body) {
            var grinches = body;

            async.each(grinches, function (grinch, callback3) {

                    async.each(presents, function (present, callback4) {

                        async.each(foods, function (food, callback5) {
                            var finalUrl = baseUrl + lang + '/comp/' + who + '/' + grinch + '/' + present + '/' + food;

                            urls.push(finalUrl);
                        }, function () {
                            callback4();
                        });

                    }, function () {
                        callback3();
                    });

                });
            }, function () {
                callback2();
            });

        });
    }, function () {
        callback();
    });

}, function () {
    for (var i = 0; i < urls.length; i++) {
        request.get(urls[i], function (err, res, body) {
            console.log('this works');
        })
    }
});

我还可以尝试什么?

使用 async.each will execute all the tasks in parallel, this might cause some performance issues specially when there are a lot of tasks in the tasks array, as a work around "though it might be slower in execution!" you might want to use async.eachSeries 确保您的所有任务 运行 一个接一个 而不是并行执行。