Skip to content

Instantly share code, notes, and snippets.

@shardulmohite
Last active August 29, 2015 13:56
Show Gist options
  • Save shardulmohite/f2956009a53d573f1e14 to your computer and use it in GitHub Desktop.
Save shardulmohite/f2956009a53d573f1e14 to your computer and use it in GitHub Desktop.
DNS look up , example of Non-Blocking function of JS .
var SiteArray = ['google.com','webonise.com','facebook.com','techcrunch.com','indiatimes.com'];
var dns = require('dns');
for(var i = 0 ; i < SiteArray.length ; i++){
dns.lookup(SiteArray[i],function(err,ip){
if (err) return handleError(err);
console.log( " %s resolved to %s ", SiteArray[i],ip );
});
}
Copy link

ghost commented Mar 3, 2014

Vishal,

following lines from your comment
Above code didn't work because dns.lookup always needs the variable i, and as its accessed by reference (in case of asynchronous callbacks, this is a must situation)

In my understating is wrong interpretation of above code because JavaScript doesn't support Passing by reference and even if it has supported that than also in above case its pass by value not pass by reference. In case of JavaScript is always pass by value, but when a variable refers to an object (including arrays), the value is a reference to the object.

So for only understanding purpose we can say that When passing in a primitive type variable like a string or a number, the value is passed in by value otherwise pass by reference.

So in above case while iterating over array and using closure we are preserving value of i i.e. we are creating a closure and invoking it.The value i inside the body of the closure is being bound to the same instance for each closure. i.e. calling self executing anonymous function as pass by value for various i

It is similar to what you are doing in example that you have given, you are passing value SiteArray[i] as value, so even value of i is changing its not effecting called function, in case of reference it might have.

Both Point are same because both has different function for executing callback code only difference is named function and anonymous.

I think asynchronous callbacks are powerful tool, this makes NodeJS so popular and faster blocking I/O has bigger issue like accessing DB could stop script until its not received any input.

Copy link

ghost commented Mar 3, 2014

Shardul,

Regarding large element in array what could be solution, I think problem remain same, I've tested above code with 15648 elements I got same issue.

When I dig more into this we all know JavaScript is Single thread programming language, and async callback or non blocking I/O works on event rather than threads or process because threads/process carry a heavy memory cost.(as Threads and process are heavier or system might not support many that much threads, this was one of reason why Nginx(event-based) become so popular than Apache(Thread or process-based on configuration) ).

So what happening behind the scene is follows :-

--Whole code runs in the main thread and main thread register async callback to event loop.(An event loop is “an entity that handles and processes external events and converts them into callback invocations”. ) At an I/O call, your code saves the callback and returns control to the node.js runtime environment. So for below line

for(var i = 0 ; i < SiteArray.length ; i++){
    dns.lookup(SiteArray[i],function(err,ip){
        if (err) return handleError(err);
        console.log( " %s resolved to %s ", SiteArray[i],ip );
    });
}

dns.lookup is register with event-loop, and execution return back to NodeJS runtime environment, this repeat with all element of array.

--After executing for loop NodeJS runtime proceed with executing other statement of function sequentially. so if you have written code below loop that has another loop that prints array you'll see that value before you started seeing callbacks console.log.

--After executing for loop or other part of code, and if any event occurred then you'll see execution of callbacks but while executing for loops any event occur on event-loop than callback will not be trigger because of Single thread and that thread is busy doing other job.

@vishaltelangre
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment