node.js - Node JS + Mongoose: saving documents inside while() -


i playing around node , mongoose , curious following issue:

i trying save documents mongo within loop / interval.

the following works fine:

setinterval(function(){      var doc = new mymodel({ name: 'test' });      doc.save(function (err, doc){         if (err) return console.error(err);         doc.speak();     });  }, 1); 

the following not work:

while(true){      var doc = new mymodel({ name: 'test' });      doc.save(function (err, doc){         if (err) return console.error(err);         doc.speak();     });  } 

what explanation behavior? save callback never firing in scenario 2

additionally, can comment on best practices building "long running workers"? interested in using node build background workers process queues of data. while() bad idea? setinterval()? additionally, plan use forever module keep process alive

thanks!

node.js single threaded while(true) occupy single thread, never giving doc.save callback chance run.

the second part of question broad though, , should ask 1 question @ time anyway.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -