3

I am newbie to Node js programming and hence want to understand the core concepts and practises very appropriately. AFAIK node js has non-blocking I/O allowing all disk and other I/O operations running async way while its JS runs in single thread managing the resource and execution paths using Event Loop. As suggested at many places developers are advised to write custom functions/methods using callback pattern e.g.

function processData(inputData, cb){
   // do some computation and other stuff

   if (err) {
    cb(err, null);
   }else{
    cb(null, result);
  }
}
callback  =  function(err, result){

 // check error and handle

 // if not error then grab result and do some stuff 
}

processData(someData, callback)
// checking whether execution is async or not
console.log('control reached at the end of block');

If I run this code everything runs synchronously printing console message at last. What I believed was that the console message will print first of all followed by execution of processData function code which will in turn invoke the callback function. And if it happens this way, the event loop will be unblocked and would only execute the response to a request when the final response is made ready by 'callback' function.

My question is:- Is the execution sequence alright as per node's nature Or Am I doing something wrong Or missing an important concept?

Any help would be appreciate from you guys !!!

2 Answers 2

2

JavaScript (just as pretty much any other language) runs sequentially. If you write

var x = 1;
x *= 2;
x += 1;

you expect the result to be 3, not 4. That would be pretty bad. The same goes with functions. When you write

foo();
bar();

you expect them to run in the very same order. This doesn't change when you nest functions:

var foo = function() {};
var bar = function(clb) {
    clb();
    foo();
};
var callback = function() {
};
bar(callback);

The expected execution sequence is bar -> callback -> foo.

So the biggest damage people do on the internet is that they put equals sign between asynchronous programming and callback pattern. This is wrong. There are many synchronous functions using callback pattern, e.g. Array.prototype.forEach().

What really happens is that NodeJS has this event loop under the hood. You can tell it to schedule something and run later by calling multiple special functions: setTimeout, setInterval, process.nextTick to name few. All I/O also schedules some things to be done on the event loop. Now if you do

var foo = function() {};
var bar = function(clb) {
    procress.nextTick(clb);  // <-- async here
    foo();
};
var callback = function() {
};
bar(callback);

The expected execution sequence is this bar -> (schedule callback) -> foo and then callback will fire at some point (i.e. when all other queued tasks are processed).

That's pretty much how it works.

Sign up to request clarification or add additional context in comments.

7 Comments

Ok. So If I wrap the usual code (written by developer) inside setTimeOut or other delayed/deferred timing functions, then the control will immediate return from there to next execution line and thus this method will make some space for event loop to attend next requests from queue. So for node js, should most of developer code be wrapped inside or must be using these special functions to allow non-blocking code execution ?
@iThirst To allow non-blocking code you have to use special functions. Note however that many libraries (e.g. database drivers) do that for you under the hood. The event loop is also accessible from C level (if you are writing C plugin). Also whether you really need non-blocking code depends on the context. Most of the time (i.e. when you are not using an external resource like db) you are fine without it.
Ok. So it turns out that the code which does not involve I/O would not affect event loop blocking noticeably. Also I tried using the mongoose library for DB interaction and evaluated its async execution using the code similar above. But in that case also console prints at last and mongoose code runs first. Does mongoose run async really in non-blocking manner even after supplying callback to its calls?
@iThirst Well, the code that does not involve I/O might affect event loop noticeably. For example if you do heavy computations (like updating complicated statistical models). But generally, yes, it is I/O that you should schedule to event loop. As for mongoose: it totally depends on the function you are using. I suggest reading docs (or source code) to be 100% sure that what it does is async or not.
Hi ! I know the question is answered with 100% satisfactory explanation. Just wanted to know that - is I use Async-Await API introduced in node js, do I still need to wrap those functions inside setTimeOut() func to make non-blocking and run in almost parallel?
|
0

You are writing synchronous code all the way down and expecting it to run asynchronously. Just because you are using callbacks does not mean it's an asynchronous operation. You should try using timers, api within your functions to feel node js non blocking asynchronous pattern. I hope The Node.js Event Loop will help you to get started.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.