I have a for loop in JS, and when I run it, it looks messy. In fact, I have a variable called "count" that is supposed to count the total loop index.
But in my code, I print it twice and these two prints have different values. I guess it's because the PDF download is an asynchronous function and therefore it doesn't run in the right order.
I would like to have two times the same value of "count" in two different places.
let count = 0;
for (let docNumber = 0; docNumber < values.length; docNumber++) {
for await (const mat of values[docNumber]) {
try {
console.log(count); // Here is a value
downloadPDF(mat.dates, "data/pdf/", count, spe, function(err, hasDownloaded) {
if (err) {
console.log(err);
} else {
if (hasDownloaded) {
console.log(count); // Here a different value
programs[count] = mat.listProgram;
matieres[count] = mat.matiere;
}
}
});
count++;
} catch (err) {
callback(err, undefined);
console.log(err);
}
}
}