Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding function (err, data) callbacks

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);
like image 839
enducat Avatar asked Dec 18 '13 01:12

enducat


People also ask

How do you read a callback function?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. The above example is a synchronous callback, as it is executed immediately.

What is error callback?

Error-First Callback in Node. js is a function which either returns an error object or any successful data returned by the function. The first argument in the function is reserved for the error object. If any error has occurred during the execution of the function, it will be returned by the first argument.

What is callback explain with an example?

"I will call back later!" A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback function can run after another function has finished.

What is the purpose of callbacks?

A callback's primary purpose is to execute code in response to an event. These events might be user-initiated, such as mouse clicks or typing. With a callback, you may instruct your application to "execute this code every time the user clicks a key on the keyboard." button.


1 Answers

The caller of the callback (which is the readFile method in this case) decides what arguments are passed to the callback. You need to declare your callback to match what readFile says that it will pass to the callback. You can name the arguments anything you want (the names you use do not matter), but they will get values in the order that readFile decides.

In this case, fs.readFile() calls the callback with the two arguments you have in your code as in callback(err, data).

Here's an example from the node.js docs:

fs.readFile('/etc/passwd', function (err, data) {
  if (err) throw err;
  console.log(data);
});
like image 196
jfriend00 Avatar answered Sep 20 '22 22:09

jfriend00