I am basically learning node.js and i am not much into advance JS concepts.
var fs = require('fs');
fs.stat('/etc/passwd', function(err, stats) {
if (err) { throw err;}
console.log(stats);
});
For the above node.js code there is an anonymous callback function, my question is how the argument values err and stats are filled up means how they get their values so that we can use them in callback function.
In Javascript, functions are first class objects and hence can be passed around as objects. Here the containing function is fs.stat and since the containing function has the callback function in its parameter as a function definition, it can execute the callback anytime.
// A function that takes two parameters, the last one a callback function
function getInput (options, callback) {
allUserData.push (options);
callback (options);
}
As you can see, in the above example, getInput provides the parameters to the callback.
Hope this explanation helps