Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda