Javascript 由于async.each存在问题,回调函数正在完成任务之前获取调用
我对async有问题。每个回调都是在完成任务之前获取调用,我有下面的代码,获取输出为 称为reprice 7在称为reprice 6之前执行,6是我构建异步中需要的数组的部分Javascript 由于async.each存在问题,回调函数正在完成任务之前获取调用,javascript,angularjs,node.js,loops,callback,Javascript,Angularjs,Node.js,Loops,Callback,我对async有问题。每个回调都是在完成任务之前获取调用,我有下面的代码,获取输出为 称为reprice 7在称为reprice 6之前执行,6是我构建异步中需要的数组的部分 called reprice : called reprice : 2 called reprice : 3 **called reprice : 7 []** no chnages has been made called reprice : 6 { status: 200, msg: 'recived optim
called reprice :
called reprice : 2
called reprice : 3
**called reprice : 7 []**
no chnages has been made
called reprice : 6 { status: 200, msg: 'recived optimal price 4 ', data: 17.75 }
called reprice : 6 { status: 200, msg: 'recived optimal price 4 ', data: 20.99 }
这是代码
async.forEach(records, function(result, callback) {
switch (result.Pricing) {
case "PF" : // current scenrio
var pfObject = _.filter(rules, {'sub_title': 'PF'});
checkRepriceFrequency(result, pfObject[0], function (response) {
if (response.status === 200) {
// call check inventory rules
CheckInventoryRules(result, pfObject[0], function (response) {
console.log('called reprice : 6 ',response);
if (response.status === 200) {
var price = result.sales_price;
var pushData = {
price: price,
optimalPrice: response.data,
SKU: result.SKU,
_id: result._id,
frequency: pfObject[0].reprice_frequency
};
productArray.push(pushData);
}
});
} else {
console.log('err');
}
});
}
callback(null);
}, function(err) {
console.log('called reprice : 7 ',productArray);
if (!err) {
if (productArray.length > 0) {
console.log('To update Synch : ',productArray);
} else {
console.log("no chnages has been made ");
}
}
});
嗯。所以您实现了
async。每个都有点错误。请参阅,async.each确保每个对象都经过循环,但不确保同步执行。在您的示例中,您调用了CheckInventoryRules
,这似乎是一个AJAX调用
但是请检查,您已经在函数末尾调用了回调(它并不等待AJAX完成)。这意味着在完成AJAX调用之前,可能已经调用了回调,表示该特定对象的处理已经完成
要修复它,您需要检查如果正在调用AJAX调用,则需要在productArray.push(pushData)之后调用callback(null)
代码>
通常,仅当任务完成执行时才调用回调。您实现的async.forEach完全错误。
开关中似乎有一个异步调用,因此您必须在该异步调用中调用回调(null)
async.forEach(records, function(result, callback) {
switch (result.Pricing) {
case "PF" :
var pfObject = _.filter(rules, {'sub_title': 'PF'});
checkRepriceFrequency(result, pfObject[0], function (response) {
if (response.status === 200) {
CheckInventoryRules(result, pfObject[0], function (response) {
console.log('called reprice : 6 ',response);
if (response.status === 200) {
var price = result.sales_price;
var pushData = {
price: price,
optimalPrice: response.data,
SKU: result.SKU,
_id: result._id,
frequency: pfObject[0].reprice_frequency
};
productArray.push(pushData);
callback(null);
}
//handle callback on else otherwise final callback will not get the event.
});
} else {
/*
callback(null);
Or
callback(Someerror);
*/
console.log('err');
}
});
break;
default:
callback(null);
// return false;
//break;
}
}, function(err) {
console.log('called reprice : 7 ',productArray);
if (!err) {
if (productArray.length > 0) {
console.log('To update Synch : ',productArray);
} else {
console.log("no chnages has been made ");
}
}
});
大家好,有人能帮我吗?谢谢@vkstack我试过这样做,但是回调是用“PF”的第一个匹配执行的,这是在switch语句中始终使用默认值的做法。但是选择总是你的。谢谢@kawadhiya21,对了,这就是问题所在,但是如果我在productArray.push(pushData),然后callback在第一次推送第一个匹配后收到调用