I am attempting to return a new array, created by combining arrays from several get requests. The new array returns correctly within the async.eachSeries
async library function, however the parent function returns undefined.
我试图返回一个新的数组,通过组合来自几个get请求的数组创建。新数组在async.eachSeries异步库函数中正确返回,但父函数返回undefined。
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. I believe I will need to run a callback, but after trying several different combinations I’m still stumped.
我一直在寻找一种方法来获取父函数get_forEachUrlString()来返回async.eachSeries函数产生的值。我相信我需要进行回调,但在尝试了几种不同的组合后,我仍然难倒。
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()
. See how I fixed this in my answer below.
编辑:我没有提到我试图在initialRender()中调用get_forEachUrlString()返回的值。请参阅下面的答案,了解我如何修复此问题。
1 个解决方案
#1
0
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()返回它应该的内容。
#1
0
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()返回它应该的内容。