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

Focus on how errors are handled:

  • The style that fs supports: One callback, where the first argument is an error (if any) and the rest are the response values.

  • The main library, with bewilderment, does not always pass the error to the first callback argument. http.get , for example.

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

+6
source share
1 answer

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

 function (err, result) 

Today it is standard.

But it also depends on what you need to β€œreturn”, as in the createServer example, where two objects are passed back to the callback.

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

This is basically an exception, and in most libraries you will see the first form.

+7
source

All Articles