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

184
Vistas
Not getting correct output when using async await inside forEach loop in Javascript

In this code, I am getting a value 34 in the newArr. Which is expected and correct.

var arr = [1, 2, 34, 5, 6, 67, 4]
var val = 34

var newArr = []

arr.forEach((elm) => {

  if (val == elm)
    newArr.push(elm)
})

console.log(newArr)

But when I am using async await I am not getting any value in newArr.

var arr = [1, 2, 34, 5, 6, 67, 4]
var val = 34

var newArr = []

arr.forEach(async(elm) => {
  await setTimeout(function() {}, 10);
  if (val == elm)
    newArr.push(elm)
})

console.log(newArr)

I am new to Javascript and not able to understand why this is not working as I am expecting.

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