cheng
cheng

Reputation: 1324

How does Node.js know if the first parameter of a callback is an error handler?

I'm quite new to Node.js and I'm trying to wrap my head around its error-first callback style.

I was curious as to how a function would know if the first parameter was for handling errors and when it's for handling requests and what not since those variable names are arbitrary.

Here are some examples of code with and without the err first

fs.readFile('/foo.txt', function(err, data) {
  if (err) {
      console.log('Ahh! An Error!');
      return;
  }
  console.log(data);
});

and

http.createServer(function(req, res) { ... });

I believe the Express framework does this by counting the number of arguments, having err as the optional first arg but in these cases, there are only two arguments.

Upvotes: 6

Views: 3160

Answers (2)

rotato poti
rotato poti

Reputation: 131

This makes it a little easier for me. Think of the function that codes the objects as having a last line something like this tacked on to the function.

if (callback && typeof(callback) === "function") {  
        callback(param1, param2);  
    }  

It will in practice be more general than that. But you can work your way down to a more general approach ( using an arguments array etc ) once you know that's there.

I got that here in the comments when I was looking for the same thing. http://www.impressivewebs.com/callback-functions-javascript/

Upvotes: 0

jfriend00
jfriend00

Reputation: 707476

Having the first argument to a callback be the error code is just a convention that node.js uses in its runtime library for all asynchronous function callbacks and has now become a somewhat standard in the node.js world for all asynchronous callbacks even in add-on modules.

You only "know" it is a convention being used because of the documentation. There is no run-time check that this is the convention being used.

FYI, this is true of all callbacks in Javascript. You only know what arguments are what by the documentation for the callback.


Note that callbacks in Javascript are used for different types of things. Some uses need to communicate an error status and some do not. For example, an http request handler is a callback, but there is no error to communicate to the callback. Either there's an incoming http request and the callback is called or there is not and it is not called. There is no error status to communicate to that type of callback so there is no error argument passed to it.

But, pretty much all callbacks that signal the completion of an asynchronous operation can end with an error and therefore must pass the err argument to the callback so the callback knows what happened and whether the async operation was successful or not.

There are two very different uses for callbacks. Only the callbacks that need to communicate a possible error status will include the err argument that you asked about.

For example, event handler callbacks (callbacks that are called when some event occurs - like a click in the browser, an incoming http request, an incoming webSocket packet, a timer event) generally don't have an error status associated with them so they don't use the err argument convention.

Upvotes: 4

Related Questions