Explanation of process.nextTick

44 views
Skip to first unread message

martypdx

unread,
Jul 21, 2011, 8:03:26 PM7/21/11
to nod...@googlegroups.com
I've looked around quite a bit and I can't find an explanation I understand as to what process.nextTick does and what I would use it for. (My knowledge of low-level details of node and threads, processes, ticks etc. is weak).  The node doc sez: "On the next loop around the event loop call this callback". There are numerous posts on high-level concept in node of event loop and writing non-blocking code and I totally get that, but it seems understanding process.nextTick means understand more specifically what exactly constitutes a loop and when the 'next' one happens.

If someone can explain or point me to a good blog post, it would be appreciated.

Thx,

-Marty

mscdex

unread,
Jul 21, 2011, 9:24:52 PM7/21/11
to nodejs
On Jul 21, 8:03 pm, martypdx <marty.nel...@yahoo.com> wrote:
> I've looked around quite a bit and I can't find an explanation I understand
> as to what process.nextTick does and what I would use it for. (My knowledge
> of low-level details of node and threads, processes, ticks etc. is weak).
>  The node doc sez: "On the next loop around the event loop call this
> callback". There are numerous posts on high-level concept in node of event
> loop and writing non-blocking code and I totally get that, but it seems
> understanding process.nextTick means understand more specifically what
> exactly constitutes a loop and when the 'next' one happens.

Well, think of it roughly as an optimized version of this:

while(stuffLeftToDo) {
checkCurrentTickTasks();
checkIOEvents();
}

When you do process.nextTick() in your code, it'll add the supplied
function to a queue to be executed at the beginning of the next
iteration of the event loop (after any I/O events have occurred in the
current tick). When the IO events are finished, the next iteration/
tick begins (if there are still things to do). Node first checks if
there are any functions queued to be executed, and executes those in
order. It then checks for I/O events, and repeats the process.
Reply all
Reply to author
Forward
0 new messages