Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
typescript loop inside promises return final counter

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 !!
})
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!