Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Loops in Node.js: Yielding Using Timers?

I'm using Node.js to loop through what could eventually be a pretty big array of posts.

If I were doing something similar using client side JavaScript, I would use timers as explained here so as not to block the thread.

My Question is: "Is still a sound practice server side?" or "Should I approach the problem differently?"

like image 916
Andrew Clarkson Avatar asked Oct 08 '12 00:10

Andrew Clarkson


2 Answers

The proper way to do that in node.js is to break up your work into chunks and use process.nextTick to queue the next chunk once the current one has completed. That way you allow other queued callbacks to be executed between each chunk of work.

UPDATE: as of Node.js 0.10, setImmediate should typically be used instead of process.nextTick for this purpose as setImmediate yields to the event loop to make sure I/O is not being starved, but process.nextTick doesn't.

like image 73
JohnnyHK Avatar answered Oct 22 '22 00:10

JohnnyHK


JohnnyHK's suggestion is valid. I would consider web workers when the task can be completed later (ie: Added to a queue).

like image 39
krg Avatar answered Oct 22 '22 00:10

krg