标题说明了一切,我正在使用async-library eachSeries
方法
let
valueOne = null,
validItem = null;
const asyncEachSeries = require('async/eachSeries');
const getContent = function (item) {
contentResponse = fetchContent(item);
return contentResponse;
}
if (recommendationsArr.length > 0) {
asyncEachSeries(recommendationsArr, function (item, callback) {
getApiContent(item).then(function getValidResult(response) {
try {
valueOne = response.content.valueOne || null; //may or may not be present
recommendationsArr.splice(recommendationsArr.indexOf(item), 1);
validItem = item;
console.log("##### valueOne is: ", valueOne); // I get data here
return true;
} catch (err) {
valueOne = null;
return false;
}
});
//I am guessing I need a return statement here, though not sure what
});
console.log("##### Outside promise, valueOne is: ", valueOne); // no data here
// Access valueOne, validItem, recommendationsArr to be able to pass as props for the component
return {
component: <Layout><ComponentName
//pass other non-dependent props
validItem={validItem}
valueOne={valueOne}
recommendationsArr={recommendationsArr}
/></Layout>,
title: `Page Title`,
};
}
return null;
场景:recommendationsArr
是一个项目数组(其99.9%不为空,但由于它是外部api调用,我更喜欢检查)。目的是为valueOne
,validItem
和更新的recommendationsArr
设置值
有效性取决于valueOne
的存在,因此如果recommendationsArr[0]
具有有效valueOne
,那么我不需要为数组的其余部分获取api结果。我正在使用eachSeries
,因为它一次只运行一个异步操作,因此如果异步获取有效结果,我不必迭代其他项。
示例建议Arr:recommendationsArr = ["blue", "white", "green", "red"]; //usually the array is anywhere between 12-50 elements
现在为了能够将更新的值传递给组件,我需要能够访问try
迭代循环之外的asyncEachSeries
块中设置的值。我知道我必须return
已处理/新值,但我不确定在什么时候以及如何返回这些值。
答案 0 :(得分:0)
与任何有关异步代码的问题一样,您可以使用callback
返回值。但eachSeries
不收集返回值。你想要的是async.mapSeries
:
async.mapSeries(recommendationsArr, function (item, callback) {
getApiContent(item).then(function (response) {
try {
valueOne = response.content.valueOne || null; //may or may not be present
recommendationsArr.splice(recommendationsArr.indexOf(item), 1);
validItem = item;
callback(null, valueOne); // This is how you return a value
// return true; // return does absolutely nothing
} catch (err) {
valueOne = null;
callback(err, valueOne);
// return false;
}
});
},function (err, result) {
// this is where you can process the result
// note that the result is an array
});
但是,由于您使用的是promises,因此您可以使用async-q
代替在承诺中包含caolan的异步库:
var asyncQ = require('async-q');
asyncQ.mapSeries(recommendationsArr, function (item) {
// Note: the return in the line below is important
return getApiContent(item).then(function (response) {
try {
valueOne = response.content.valueOne || null; //may or may not be present
recommendationsArr.splice(recommendationsArr.indexOf(item), 1);
validItem = item;
return valueOne; // this is how you return a value in a promise
} catch (err) {
return null
}
});
})
.then(function(result) {
// you can process the result here
});
我个人更喜欢重写代码并最终处理结果,因为代码更清晰:
asyncQ.mapSeries(recommendationsArr, getApiContent)
.then(function (result) {
for(var i=0;i<result.length;i++) {
var response = result[i];
// do what needs to be done with each response here
}
});