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.