[英]Node.js call callback function inside for loop
我试图在for
循环中调用一个函数,问题是在循环完成后调用该函数。
以下面的例子为例,它打印到控制台:
here1
here1
here2
here2
代替
here1
here2
here1
here2
report.forEach(item => {
item.runs.forEach(run => {
waComplianceBusiness(req, run.id, (err, res) => {
const compliance = res.data.overviews[0].compliance;
var failureList = [];
compliance.forEach((rule, index) => {
console.log('here1');
waRuleOverview(req, run.id, rule.id, (err, res) => {
console.log('here2');
// handle the response
});
});
});
});
});
我怎样才能解决这个问题?
如果我需要提供更多信息,请与我们联系
这是完整的代码:
export default (req, callback) => {
const report = req.body.webAudits;
if(report.length > 0) {
report.forEach(item => {
item.runs.forEach(run => {
waComplianceBusiness(req, run.id, (err, res) => {
const compliance = res.data.overviews[0].compliance;
if(compliance) {
var failureList = [];
compliance.forEach((rule, index) => {
if(rule.pagesFailed > 0) {
waRuleOverview(req, run.id, rule.id, (err, res) => {
const failedConditions = res.data.failedConditions;
const ruleName = res.data.ruleName;
failedConditions.forEach((condition, failedIndex) => {
const request = {
itemId: condition.conditionResult.id,
itemType: condition.conditionResult.idType,
parentId: condition.conditionResult.parentId,
parentType: condition.conditionResult.parentType
}
const body = {
runId: run.id,
ruleId: rule.id,
payload: request
}
waConditionOverview(req, body, (err, res) => {
const description = res.data.description;
const conditionValues = res.data.conditionValues[0];
var actualValue = conditionValues.value;
if(actualValue == "") {
actualValue = 'empty';
}
if(description.idType == "variable") {
var failureObj = {
ruleName: ruleName,
expected: description.name + ' ' + description.matcher + ' ' + description.expected[0],
actual: description.name + ' ' + description.matcher + ' ' + actualValue
};
}
else if(description.idType == "tag") {
var failureObj = {
ruleName: ruleName,
expected: description.name + '\n' + description.matcher,
actual: actualValue
};
}
failureList.push(failureObj);
});
});
});
}
if(key + 1 == compliance.length) {
console.log(failureList);
}
});
}
});
});
});
}
}
这些是回调函数:
export function waComplianceBusiness(req, runId, callback) {
const apiToken = req.currentUser.apiToken;
const payload = {
'Authorization': 'api_key ' + apiToken
}
const options = {
'method': 'get',
'gzip': true,
'headers': payload,
'content-type': 'application/json',
'json': true,
'url': 'api_url'
}
request(options, (error, response, body) => {
callback(null, body);
});
}
export function waRuleOverview(req, runId, ruleId, callback) {
const apiToken = req.currentUser.apiToken;
const payload = {
'Authorization': 'api_key ' + apiToken
}
const options = {
'method': 'get',
'gzip': true,
'headers': payload,
'content-type': 'application/json',
'json': true,
'url': 'api_url'
}
request(options, (error, response, body) => {
callback(null, body);
});
}
export function waConditionOverview(req, body, callback) {
const apiToken = req.currentUser.apiToken;
const payload = {
'Authorization': 'api_key ' + apiToken
}
const options = {
'method': 'post',
'gzip': true,
'headers': payload,
'body': body.payload,
'content-type': 'application/json',
'json': true,
'url': 'api_url'
}
request(options, (error, response, body) => {
callback(null, body);
});
}
我的目标是在完成compliance
数组的循环之后返回failureList
数组
for循环按顺序执行作用域内的语句。 但它不等待函数调用完成,它继续下一个语句(即异步工作)。 这就是为什么结果如此。 您可以使用Promises
或使用async
模块使其同步工作。
由于目前尚不清楚您将在函数调用中执行什么以及您希望语句执行什么,因此我无法建议其中任何一个。 。 asyn.each
通常是使for循环同步执行的首选。 当您想要等待函数完成执行然后执行操作时,将使用promise。 您可能想查看他们的文档
谢谢你,Ragul
如果要按顺序执行,请使用async.eachOfSeries
async.eachOfSeries(report, function(item, index, eachOfCallback1){
async.eachOfSeries(item.runs, function(run, index, eachOfCallback2){
waComplianceBusiness(req, run.id, (err, res) => {
var failureList = [];
async.eachOfSeries(compliance, function(rule, index, eachOfCallback3){
console.log('here1');
waRuleOverview(req, run.id, rule.id, (err, res) => {
console.log('here2');
return eachOfCallback3(err);
});
}, function(err){
if(err)
return eachOfCallback2(err);
else return eachOfCallback2();
});
});
}, function(err){
if(err)
return eachOfCallback1(err);
else return eachOfCallback1();
})
}, function(err){
// handle final response
})
如果要优化该过程,请查看async.parallel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.