Tuesday 13 March 2018

How to run a for loop synchronously in node JS and perform an operation, without loop moving forward?

Hi, So I'm stuck with a node JS problem. This is due the the async nature of node, I know that. But my problem is, I have an array, which I want to loop over, use the elements in the array to search for documents in mongoDB and then push the results in another array. My problem is that sometimes the array is returned empty when I log it after the for loop. For now, I've added a counter to check if the length of the final array is long enough, and then only move ahead. Here's my code:http://ift.tt/2GsQcJX sure that I haven't explained my problem properly, but I hope you get an idea. ThanksEDIT: I've heard about the async library, but really haven't understood the usage. If someone could explain in the above context, it'd be great!

Submitted March 13, 2018 at 04:49PM by mclovin4009

No comments:

Post a Comment