Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the proper way to document callbacks with jsdoc?

I've spent quite a while scouring the internet looking for the best way to properly document callbacks with jsdoc, but unfortunately, I haven't found a great one yet.

Here's my question:

I'm writing a Node.js library for developers. This library provides multiple classes, functions, and methods that developers will be working with.

In order to make my code clear and understandable, as well as to (hopefully) auto-generate some API documentation in the future, I've started using jsdoc in my code to self-document what's happening.

Let's say I define a function like the following:

function addStuff(x, y, callback) {   callback(x+y); }); 

Using jsdoc, I'm currently documenting this function as follows:

/**   * Add two numbers together, then pass the results to a callback function.   *   * @function addStuff   * @param {int} x - An integer.   * @param {int} y - An integer.   * @param {function} callback - A callback to run whose signature is (sum), where   *  sum is an integer.   */ function addStuff(x, y, callback) {   callback(x+y); }); 

I feel like the above solution is kinda hack-ish, since there's no way for me to specify in absolute terms what the callback function should accept.

Ideally, I'd like to do something like:

/**   * Add two numbers together, then pass the results to a callback function.   *   * @function addStuff   * @param {int} x - An integer.   * @param {int} y - An integer.   * @param {callback} callback - A callback to run.   * @param {int} callback.sum - An integer.   */ function addStuff(x, y, callback) {   callback(x+y); }); 

The above seems like it'd allow me to more simply convey what my callback needs to accept. Does that make sense?

I guess my question is simple: what's the best way to clearly document my callback functions with jsdoc?

Thank you for your time.

like image 216
rdegges Avatar asked Jun 13 '14 23:06

rdegges


People also ask

How do you explain callbacks?

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

Where are callbacks stored?

I have learned that function callbacks are stored on a queue usually referred to as the event queue or message queue, and that this is what allows JavaScript to operate asynchronously.

What are JSDoc comments?

JSDoc comments are used for documentation lookup with Ctrl+Q in JavaScript and TypeScript, see JavaScript documentation look-up and TypeScript documentation look-up, as well as for type annotations and method return type hints in chained methods.

How do you execute a callback?

A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.


1 Answers

JSDoc 3 has a @callback tag for exactly this purpose. Here's a usage example:

/**  * Callback for adding two numbers.  *  * @callback addStuffCallback  * @param {int} sum - An integer.  */  /**  * Add two numbers together, then pass the results to a callback function.  *  * @param {int} x - An integer.  * @param {int} y - An integer.  * @param {addStuffCallback} callback - A callback to run.  */ function addStuff(x, y, callback) {   callback(x+y); } 
like image 192
Jeff Williams Avatar answered Sep 23 '22 12:09

Jeff Williams