Reputation: 681
For an application where each request may take a second or two, is it possible to only process a piece of operative code on each iteration of the event loop? For example:
function foo()
{
...operative code...
...start processing the next event here...
...continue with foo() here....
}
Would it be something like this?
function foo()
{
...operative code...
process.nextTick(function() {
...continue with foo() here...
});
}
And if that is the way to do it, does Node automatically start processing whichever event is next in the queue?
Upvotes: 0
Views: 151
Reputation: 8770
Your assumption is correct, you should be dividing the work into smaller blocks of execution and schedule the next block with process.nextTick
. All the other scheduled events that need to be executed at the next tick will be handled prior to your block of required execution.
Upvotes: 1
Reputation: 14379
If the time is spent in IO, node's non-blocking model will automatically handle concurrency.
If it's not spent in IO, then you're right in using process.nextTick to defer the execution of your code so that other requests have a change to be handled. Here is a good writeup:
http://howtonode.org/understanding-process-next-tick
Upvotes: 1