Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 });

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading