Search code examples
node.jsblockingnonblocking

What is a blocking operation in node js exactly?


I have been reading quite a bit about blocking and non-blocking operations in node.js, and so far this is what I've put together:

database operations --> non-blocking;
open/close files -----> non-blocking;
network operations ---> non-blocking;

My big doubt is about "normal" operations, that is, those operations which do not require access to anything more than the RAM. In fact, let's say I run a long, long, long set of these "normal" operations in my node server. Is there a point at which they are so many that their execution might actually decrease the performance of the server? (obviously, I am just a beginner, so don't be too harsh).


Solution

  • There is no magic "non-blocking" call in javascript or node.js, so the notion of "blocking" or "sync" function call depend a lot on what is desired effect of a function. All function calls in javascript (with exception for ES6 generators) are synchronous, so I'd define "non blocking" call as follows:

    Function often referred as "non-blocking" if at the moment function return control to parent desired side effect may not be ready yet. Instead, some state is changed in the runtime (commands put in the queue, file descriptors added to watch list etc) and runtime may resume control to another function referenced by "non-blocking" function as a parameter ( often called "callback" function)