Node.js is single threaded. That means that there is no parallelism. Everything happens sequentially. If data event fires then the associated handler will fire and nothing else will run at the same time. Every block of code (i.e. code between brackets {...}
) will run sequentially and nothing can interrupt it.
this piece of code is asynchronous, due to the private function passed
in the CreateServer method
That's actually not true. Have a look at this example:
var fn = function(clb) {
clb(1);
}
console.log("start");
fn(function(x) {
console.log(x);
});
console.log("end");
fn
function accepts a callback but it is not asynchronous. The function you were talking about is asynchronous because we know that it is asynchronous. There are several asynchronous functions in Node.js: process.nextTick
, setTimeout
, setInterval
, lots of I/O function (files, network, etc.). Combination of these asynchronous functions is also asynchronous. But without knowing what a function does you can't assume a priori that it is asynchronous. The real life example of a synchronous function with a callback is .forEach
method on lists.