简体   繁体   English

如何在函数内的async.eachSeries循环内从多个get请求返回响应

[英]How do I return a response from multiple get requests, inside an async.eachSeries loop, inside a function

I am attempting to return a new array, created by combining arrays from several get requests. 我正在尝试返回一个新数组,该数组是通过组合几个get请求中的数组而创建的。 The new array returns correctly within the async.eachSeries async library function, however the parent function returns undefined. 新数组在async.eachSeries 异步库函数中正确返回,但是父函数返回未定义。

My understanding is that the return within the parent function is running synchronously with the async.eachSeries() (before it can finish), which causes the parent function to return undefined . 我的理解是,父函数内的返回与async.eachSeries()同步运行(在完成之前),这导致父函数返回undefined

var array_strings = [ “someUrl”, “someUrl”, “someUrl” ]

function get_forEachUrlString(base_url, array_strings) {
    …
    var combinedArray = [];
    var mergedArray = [];
    …
    async.eachSeries(array_strings, function(item, done) {
        …
        $.get(bas_url+item, function(data) {
            …
            combinedArray.push(data)
            done(); // stopping the async looping
            mergedArray = [].concat.apply([], combinedArray); // Merging the array of combined arrays
        });
    }, function(err) {
        if (err) {
            throw err;
        }
        console.log("All requests are done");
        console.log(mergedArray); // logs correct array
        return mergedArray
    });
    console.log(mergedArray); // logs [] empty array
    return mergedArray // Hoping this would return the newly created mergedArray
}

get_forEachUrlString(“someBaseUrl”, array_strings) // Attempting to return mergedArray

// this function tries to call the mergedArray, but returns undefined
function initialRender() {
    …
    console.log(merged) // returns undefined
} 

I have been searching for a way to get the parent function get_forEachUrlString() to return the value produced by the async.eachSeries function. 我一直在寻找获取父函数get_forEachUrlString()以返回async.eachSeries函数产生的值的方法。 I believe I will need to run a callback, but after trying several different combinations I'm still stumped. 我相信我将需要运行回调,但是在尝试了几种不同的组合之后,我仍然很困惑。

How can I get the new array I've created, mergedArray to return from the function get_forEachUrlString() ? 如何获取我创建的新数组mergedArray以从get_forEachUrlString()函数返回?

EDIT: I failed to mention that I am attempting to call the returned value from get_forEachUrlString() within initialRender() . 编辑:我没有提及我正在尝试从get_forEachUrlString()中的initialRender()调用返回的值。 See how I fixed this in my answer below. 请参阅下面的答案,了解如何解决此问题。

After some trial and error I was able to get this working. 经过一番尝试和错误,我能够使它工作。 I was attempting to place any number of callbacks, but what I really needed to do was place the initialRender() function within the get_forEachUrlString() function and pass the new mergedArray value to the initialRender() function. 我试图放置任意数量的回调,但是我真正需要做的是将initialRender()函数放置在get_forEachUrlString()函数中,并将新的mergedArray值传递给initialRender()函数。

So the proper setup looks like this. 因此正确的设置如下所示。

function get_forEachUrlString(base_url, array_strings) {
    …
    async.eachSeries(array_strings, function(item, done) {
        …
    }, function(err) {
        …
        mergedArray = [].concat.apply([], combinedArray);
        initialRender(mergedArray);
    });
}

get_forEachUrlString(“someBaseUrl”, array_strings)

Then the initialRender() returns what it should. 然后initialRender()返回它应该是什么。

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

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