Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the suggested callback style for Node.js libraries?

Tags:

node.js

With focus on how errors are handled:

  • There's the style that fs promotes: One callback where the first argument is an error (if any), and the remaining are response values.

  • The core library confusingly doesn't always pass an error to the first argument of a callback. http.get, for example.

  • Another style is to have two callbacks (callback & errback). Promoted by http://howtonode.org/control-flow-part-ii

like image 723
Nevir Avatar asked Aug 19 '11 03:08

Nevir


1 Answers

I will certainly say that in most cases, you will see the following signature for callbacks.

function (err, result)

This is pretty much standard today.

But it also depends on what you need to "return" like in the createServer example where two objects are passed back to the callback.

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

This is mostly the exception and in most libraries you will see the first form.

like image 128
theprogrammer Avatar answered Oct 11 '22 08:10

theprogrammer