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

Instruction not waiting properly NodeJS

I was wondering what is the problem here, why is the return statement not waiting to finish the previous instruction? orders are empty, even though I’m logging inside the getOrders to make sure there were data (there is data).

await stores.forEach(async (store) => {
  let arr = await getOrders(store, token, tokenType);
  orders.push(arr);
})
return orders;

>Solution :

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

To wait for all promises, you need an array of promises, which you can get using map. Then you need to use Promise.all, so that it will wait for all the promises in that array to complete.

await Promise.all(stores.map(async (store) => {
  let arr = await getOrders(store, token, tokenType);
  orders.push(arr);
}));
return orders;
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