Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document callbacks using JSDoc?

Tags:

jsdoc

Given a Javascript function that takes callback functions as parameters:

var myFunction = function(onSuccess, onFailure) {...} 

How do I document onSuccess's return type and arguments?

like image 521
Gili Avatar asked Nov 15 '12 18:11

Gili


People also ask

What is callback fn?

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.

What is callback () in JavaScript?

A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback function can run after another function has finished.

How JavaScript callbacks are implemented?

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.

What is fallback function in JavaScript?

js applications. A fallback function specifies an action that is performed whenever a call to a remote service fails. For example, you can use a fallback function to send a customized message about service failures to the client.


2 Answers

In JSDoc 3.1 and later, you can use the new @callback tag to describe the callback function in a separate comment block. You can then refer to the callback in the docs for your method.

Here's an example:

/** @class */ function MyClass() {}  /**  * Do something.  * @param {MyClass~onSuccess} cb - Called on success.  */ MyClass.prototype.myFunction = function(cb) {     // code };  /**  * Callback used by myFunction.  * @callback MyClass~onSuccess  * @param {number} resultCode  * @param {string} resultMessage  */ 

See Use JSDoc for more information.

like image 56
Jeff Williams Avatar answered Sep 22 '22 09:09

Jeff Williams


It seems this functionality does not exist yet.

This functionality was added as of JSDoc 3.1. See:

  • http://code.google.com/p/jsdoc-toolkit/issues/detail?id=319
  • https://github.com/jsdoc3/jsdoc/issues/260

for a related discussion.

like image 23
Gili Avatar answered Sep 21 '22 09:09

Gili