'Calling callback function inside for loop doesn't work
I am trying to run a callback function inside a for loop, but I am finding issues in executing the callback function. Seems like the function gets executed after the for loop is completed. For example if for loop has 2 iteration, then the function is executed only after the two for loop iteration is executed. Hence the execution of function is always done after the last iteration of for for loop. I am trying it really hard and have spend almost a day today looking at examples and have finally have used the closures as per the example provided in example in below link but still not getting the accurate result. https://www.geekabyte.io/2013/04/callback-functions-in-loops-in.html
Below is the code that I am using :
let promoList = [];
for(i = 0; i< availablePromoCount; i++){
promoObj = {};
promoObj.maxUsesUser = getAvailablePromoResults[i].MaxUsesUser //getAvailablePromoResults is a list coming from another function not shown here
promoObj.promoCode = getAvailablePromoResults[i].PromoCode; // available promo code for this iteration
promoList.push(promoObj);
(function(clsn){
searchUserUsedPromo(userId, promoList[clsn].promoCode, (error, searchUserUsedPromoResults) => {
if (error) {
console.log(error);
return res.status(500).json({
success: 0,
message: "Some other error",
error: error,
});
}
console.log("Iteration of function is ", clsn); // always iteration is last iteration and displays same and last iteration of i
console.log("Searched promo code is ", promoList[clsn].promoCode);
timesUsed = searchUserUsedPromoResults.length;
console.log("Times Used", timesUsed);
console.log("Max uses user", promoList[i].maxUsesUser);
});
})(i)
count++;
if(getAvailablePromoResults.length == count){
return res.status(200).json({
success: 1,
availablePromoList: availablePromoList,
});
}
}
searchUserUsedPromo() calls order.model.js to get the db query and executes
searchUserUsedPromo: (userId, promoCode, callback) => {
pool.query(
`select * from userpromo where UserId = ? and PromoCode = ? and Used = 1`,
[
userId,
promoCode
],
(error, results, fields) => {
console.log(results);
if (error) {
return callback(error);
}
return callback(null, results);
}
);
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
