javascript - Mongoose find() call inside for loop using a latch -


i've been on hours , can't seem find answer. problem have call mongodb inside loop. i'm using latch waits call end before advancing again. here's code:

var latch = true; (var i=0; i<array.length; i++) {     while(latch == false){}     table1.find({}, function(err, result){         ... code ...         latch = true;     });     latch = false; } 

the problem doesn't run callback table1.find(), gets blocked on while. can me this?

the loop never proceed past while loop (as you've created infinite loop).

there several ways handle async code within loops in node, including counter variables outside function, , tail-recursion. can see examples here: http://metaduck.com/01-asynchronous-iteration-patterns.html

i big fan of https://github.com/caolan/async provides async.each applies iterator each element in parallel. suit purpose.


Comments

Popular posts from this blog

java - HTTP Status 500 - An exception occurred processing JSP page /second.jsp at line 15 -

php - Using str_replace to translate a MySQL Table in html -

SQL Server - MyCTE query based on 24 hour period (next day) -