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

185
Vistas
Promise returns after awaiting in map function but data returns when awaiting outside map function

I am fetching userData from MongoDB by passing userId then awaiting for userData. Its returning userData if I request data without an array of ids. it gives me data. but if I run a loop on Array of ids and await that It gives me an array of promises but not an array of user data. why is that happening?.

I know how to solve it by await Promise.all(promiseArray).

but my question is why is this behavior? and also when I try to console the user in the map function it consoles userData, not the promise (on line 156). and I have learned that await stop the execution wait for the line that is awaiting to run then after that i continues. but here its not the case. its not stopping the map function instead if doesn't run code after the await but showing the behavior of continue keyword at await. and then after when the promise of await if fulfilled then code after that runs.

here is the code

// this.guides =  ["userIdOne","userIdTwo"]
tourSchema.post("save", async function (doc, next) {
  const guideSingle = await User.findById(this.guides[0]);

  const guideArray = this.guides.map(async (id) => {
    console.log("ID: ", id);
    let user = await User.findById(id);
    console.log("USER: ", user);
    return user;
  });

  console.log("SINGLE GUIDE: ", guideSingle);
  console.log("ARRAY GUIDE:", guideArray);
  next();
});

here I am adding output of this code. enter image description here

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