Re: [nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread linuxha
On Fri, Apr 6, 2012 at 10:25 PM, Tim Price wrote: > You need to think about how the loop blocks the stack. All code "block" , > its just how you block that counts. All the threads in the world wont help > you if you don't know how to break the stack. What nextTick does is break > the stack, so it

Re: [nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread Tim Price
You need to think about how the loop blocks the stack. All code "block" , its just how you block that counts. All the threads in the world wont help you if you don't know how to break the stack. What nextTick does is break the stack, so it adds the function to the next run around nodes event loo

Re: [nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread Scott Elcomb
On Fri, Apr 6, 2012 at 8:40 PM, linuxha wrote: > On Fri, Apr 6, 2012 at 5:59 PM, Tim Price wrote: >> Maybe could be used to traverse mongoose docs? > > ??? Not sure what this is for "Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment."

Re: [nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread linuxha
On Fri, Apr 6, 2012 at 5:59 PM, Tim Price wrote: > What about this for an aync for loop > > var forLoop = function(data, worker, callBack) { > if(Array.isArray(data)) { > var returnData = []; > var loop = function(i, cb) { > if(i === data.length) { > return callBack(null, returnData); > } > proces

[nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread Tim Price
What about this for an aync for loop var forLoop = function(data, worker, callBack) { if(Array.isArray(data)) { var returnData = []; var loop = function(i, cb) { if(i === data.length) { return callBack(null, returnData); } process.nextTick(function() { returnData.push(worker.call(data[i++])) loop(

Re: [nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread Tim Caswell
If what you're doing is event based (listening for a user to press a button, flip a switch) or time based (want to poll a battery level every 100ms) Don't write blocking code in a thread, use the many event primitives that node provides. You can write your API as a subclass to EventEmitter. The

Re: [nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread Bruno Jouhier
And you can now write CPU intensive functions directly in Javascript, and run them in a separate thread: https://github.com/xk/node-threads-a-gogo. On Friday, April 6, 2012 9:22:29 PM UTC+2, Tim Caswell wrote: > > Keep in mind that the nextTick hack technique still blocks your CPU. It's > jus

Re: [nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread linuxha
On Fri, Apr 6, 2012 at 3:22 PM, Tim Caswell wrote: > Keep in mind that the nextTick hack technique still blocks your CPU.  It's > just broken up into many small parts.  If you must do something that's truly > CPU intensive, put it on a thread or another process.  Most modern machines > (even phone

Re: [nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread Tim Caswell
Keep in mind that the nextTick hack technique still blocks your CPU. It's just broken up into many small parts. If you must do something that's truly CPU intensive, put it on a thread or another process. Most modern machines (even phones) have multiple CPU cores. Interprocess communication is a

[nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread mscdex
On Apr 6, 2:38 pm, Mark Hahn wrote: > In any case, here is a loop that runs until cond is true ... > > function processOneTick() { >     // do work for one tick >     if (!cond)  setTimeout processOneTick, 0} > > processOneTick() May as well use process.nextTick() instead for that approach. --

[nodejs] Re: Need to run a loop in node.js

2012-04-06 Thread mscdex
On Apr 6, 12:38 pm, Marak Squires wrote: > Are you familiar with JavaScript at all? It has built in keywords for > looping ( such as "for" ) > > Check outhttp://eloquentjavascript.net/chapter2.html > Also: https://developer.mozilla.org/en/JavaScript/Guide -- Job Board: http://jobs.nodejs.org/ P