Im doing a loop with promises and want to return the final counter, the thing is that when I check the counter inside is correct, but when I check the value resolved is always 0.
so here is the idea
let tempFinalResult: number
export function finalRecount(): Promise<number> {
tempIssues = 0 // Restart the counter on call the function
return new Promise<number>((resolve) => {
getStoredValues().then((resultsFound) => {
resultsFound.forEach((query) => {
fetchValuesFromQuery(query.query) // call external api for get values
.then((queryResults) => {
console.log(
'Values found' +
queryResults.values.length // This is working founds the data
)
tempFinalResult += queryResults.values.length
})
.catch((err) => {})
})
console.log('Total values ' + tempFinalResult) // oh oh the values are 0
resolve(tempFinalResult)
})
})
}
Now when I call the function
finalRecount.then((counter) => {
console.log(counter) // Is 0 !!
})