I could not get the index right of for loop when I asynchronous function inside a for loop. Here is my code:
for (var i = 0; i < details.waypoints.meta.length; i++) {
for (var j = start_index; j < i; j++) {
(function (cntr) {
fs.readFile(Path.join(__dirname, "/../../Modified_Data.json"), "utf8", function readFileCallback(err, data) {
if (err) {
console.log(err);
return;
} else {
console.log(cntr) //printing the index
}
});
})(j);
}
}
The index I am getting are which are not in right order: 1 3 0 2 4 5 6 7 8 9