I am facing an issue related to the variable scope. Initially set "successCount" and "failedCount" variables as '0' and incremented that value after each success/failure iteration. But after the iteration got the initial value only for those variables.
var successCount = 0;
var failedCount = 0;
var counter = 0;
var data = {};
for (var i = 0; i < 5; i++) {
updateField(data, (response) => {
if (response.status) {
successCount++;
} else {
failedCount++;
}
});
counter++;
}
Accessing variable outside the loop
if(counter === 5) {
console.log(successCount);// Value is still 0
console.log(failedCount);// Value is still 0
}
Any solution or what I am wrong with this code.
Thanks in advance