javascriptnode.jscallbackeventqueue

processing the next event in Node.js


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?


Solution

  • 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