5

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!

3
  • 1
    The standard node callback form of callback(err, result) is used by the node libraries and by libraries attempting to be consistent with that style. However, there is no obligation by library authors to follow it. Thus, if you are looking at userland code, you may or may not find it being used. Commented May 27, 2016 at 17:38
  • Can you show some examples where it's not being used? Commented May 27, 2016 at 18:11
  • For example, when using some modules : var net = require('net'); var server = net.createServer(function(connection) { connection.on('end', function() { console.log('client disconnected'); }); });` Commented May 27, 2016 at 18:41

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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) {
  ...
});

1 Comment

Thanks! But I noticed that in express, err parameter is not often used. Like this simple code: var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World'); }), why does the callbacks not take err?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.