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

后端 未结 4 862
春和景丽
春和景丽 2021-01-30 04:52

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.

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 05:25

    Another possibility is to describe the value passed to the callback this way:

    /**
      * 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(int)} callback - A callback to run whose signature is (sum), where
      *  sum is an integer.
      */
    function addStuff(x, y, callback) {
        callback(x+y);
    });
    

    To document the return type of the callback, use @param {function(int):string}.

提交回复
热议问题