So I am confused about how the function(err, data) callback works, is the first argument always an error handler?
What about the remaining arguments if you had something like function (x, y, z, a, b, c)?
How does the data from fs.readFile pass from the top line of code to the bottom line of code? Or in other words, how does the output of fs.readFile get put into the data argument?
fs.readFile(pathToFile, function (err, **data**) {
bufferString = **data**.toString();
I could replace function (err, data) with function (x, y) and function (x, y, z, a, b, c)
But only the second argument works (data and y), is this just the syntax of javascript callbacks?
For example, this is working code to asynchronously read a file and print out the number of lines given a file:
var fs = require('fs');
var pathToFile = process.argv[2];
var bufferString, bufferStringSplit;
function counter(callback) {
fs.readFile(pathToFile, function (err, data) {
bufferString = data.toString();
bufferStringSplit = bufferString.split('\n');
callback();
});
}
function logMyNumber() {
console.log(bufferStringSplit.length-1);
}
counter(logMyNumber);