Monday, March 1, 2021

How to document function callbacks using JSDoc

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);

}



References:

https://stackoverflow.com/questions/24214962/whats-the-proper-way-to-document-callbacks-with-jsdoc

No comments:

Post a Comment