diodeBucks
diodeBucks

Reputation: 173

nodeJS callback error parameter

I'm learning node now and I'm confused about the err parameter. I thought it's supposed to be the first argument of a callback function but I don't see it in many call back functions. Can anyone explain it to me? Thanks!

Upvotes: 5

Views: 2208

Answers (3)

jack blank
jack blank

Reputation: 5195

The error parameter is usually for asynchronous code. node errors

Most asynchronous methods that accept a callback function will accept an Error object passed as the first argument to that function. If that first argument is not null and is an instance of Error, then an error occurred that should be handled.

app.get() sends get request and return an error like a 404

and you could do something like this res.status(404).render( in app.get()

Express error handling

error-handling functions have four arguments instead of three: (err, req, res, next)

The reason why some code uses err as the first parameter is because some code like fs.readFileis programmed to check if there was an error and to handle it. The author of the API specifically wrote code to check the first argument for an error and handle it. That's why it is available to you for some methods an unavailable for other methods.

Upvotes: 1

tadman
tadman

Reputation: 211610

There's many different kinds of functions and callback functions in particular. The Node.js standard for callback functions is those of the form:

function(err, arg1, arg2, ...)

Where arg1 and so forth are only present if relevant but the err argument is always first. This is the reverse of a lot of historical JavaScript code where errors would be the last argument.

The Node.js method of forcing the error as the first argument even if there's no error makes ignoring errors harder, you rarely forget to declare that argument, and makes their location predictable.

Now this only applies in the case of a general-purpose callback. That is, there are occasions where calling a function will trigger a singular callback at some point in the future. You'll see them used like this:

doStuff(function(err, successValue) { ... });

There's also the style popularized by jQuery where one or more of your callbacks will be triggered depending on the outcome of the operation:

doStuff({
  success: function(successValue) { ... },
  error: function(err) { ... },
  timeout: function() { ... }
});

Note that in this case you may have both the error and timeout callbacks being fired. You're not obligated to populate all of these, either.

The downside to this approach is the unpredictability of which ones get called and the risk of handling something twice inadvertently.

Upvotes: 1

robertklep
robertklep

Reputation: 203359

First: a callback is just a function. Different callbacks serve different purposes.

In general, a function that performs an asynchronous action and should "return" a value gets passed a callback function that will take (at least) two arguments: the first is used to pass errors (if any), the second (and following) are used to pass the value(s) that should be returned to the caller.

You noticed that net.createServer() will also take a callback function, but that function only has one argument.

That's because, in this case, the callback isn't used to pass errors and/or values. Instead, it's a function that gets called when a new connection is made to the server.

It's really a bit of a shortcut. This code:

var server = net.createServer(function(connection) {
  ...
});

Is short for this code:

var server = net.createServer();
server.on('connection', function(connection) {
  ...
});

Upvotes: 0

Related Questions