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

98
Vistas
Js await function inside a map

I have an array of category ids in order, I have to make a map and fetch the items from these categories and in the end keep the original order of the array

what I've been doing:

const info = []
Promise.all(categoryItem.next_steps.map((next_step) => {
   return categoryItemSvcV2.getAllItensNoOrderByCategoryId(next_step.category, idpage)
       .then((result) => {
            info.push(result)
       })
})).then(() => {
   res.json({ success: true, info })
})

the categoryItem.next_steps is the array of categoryIds on order, however each time I call this function it displays in an order

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Promise.all does this out of the box, don't bring your own array:

Promise.all(categoryItem.next_steps.map(next_step =>
  categoryItemSvcV2.getAllItensNoOrderByCategoryId(next_step.category, idpage)
)).then(info => {
//      ^^^^
   res.json({ success: true, info })
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

As you're using Promise.all, you don't need to create a separate array. Looking at your code above, this should work

const values = await Promise.all(
  categoryItem.next_steps.map((next_step) =>
    categoryItemSvcV2.getAllItensNoOrderByCategoryId(next_step.category, idpage)
  )
);

res.json({ success: true, values });

about 4 years ago · Juan Pablo Isaza Denunciar
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