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

Using promises stops variable being set

The following code sets the row.already_messaged_clients absolutely fine and is accessible outside of the loop.

businesses.forEach(async function(row) {
// let already_messaged_clients_arr = await getAlreadyMessagedClientsForPlaceID(row.place_id);
row.already_messaged_clients = 3;
});

When I use an await like this:

businesses.forEach(async function(row) {
let already_messaged_clients_arr = await getAlreadyMessagedClientsForPlaceID(row.place_id);
row.already_messaged_clients = 3;
});

row.already_messaged_clients does not get set. Why is this?

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

Thanks for your help

>Solution :

you can use a ‘for…of’ loop instead of ‘forEach’

for (let row of businesses) {
  let already_messaged_clients_arr = await 
  getAlreadyMessagedClientsForPlaceID(row.place_id);
  row.already_messaged_clients = 3;
}
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