I was just getting ready to get my hands rough with Nodejs, but while experimenting and playing a bit on the first example, i found some behaviour which I am finding hard to understand.
var a = function () {
console.log('print 3');
return 5000;
};
setTimeout( function (){
console.log('print 2');
}, a()
);
console.log('print 1');
The output to the above code is:
print 3 print 1 print 2
AND a stupid doubt is, while the above code works, this one doesn’t.
setTimeout( function (){
console.log('print 2');
}, (function () {console.log('print 3'); return 5000;} )
);
console.log('print 1');
Please explain the above behaviour.
Formatting the code better should help you understand what is happening.
The following snippet does not work as you never execute the 2nd part of the function.
The following will work:
As an aside
The example has nothing to do with node.js and will produce the exact same results in any browser that responds to console.