58

I am using JSDoc for parameter documentation.

It is clear how to document the parameter types for many_prompts, but what is the right way to document the function it returns?

/**
 * @param {Number} - number of times to prompt
 * @return {Function(prompt{Number})} - the returned function
 */
function many_prompts(count) {
  return function(prompt) {
    for(var i=0; i < count; i++) alert(prompt);
  }
}


//Example of use:
var y  =many_prompts(3);
y('Hello World');

4 Answers 4

26

This seems to be working for me.

 /**
 * @param {Number} count - number of times to prompt
 * @return {function(): void} - the returned function
 */
  manyPrompts(count) {
      /**
       * My inner function
       *
       * @param {object} prompt Some parameter
       */
      const inner = function(prompt) {
        for (let i=0; i < count; i++) {
          alert(prompt);
        };
      };
      return inner;
  }
3
  • You also don't need the parentheses: @return {function: void} works Commented Jun 11, 2021 at 19:38
  • This seems to work in VS Code, but doesn't pick up on the inner function documentation. :( Commented May 17, 2022 at 21:34
  • I was able to use this one with parameters inside the parentheses: @returns {(function([string], string, *, *): void)}, doesn't seem to be possible to name them, though, but the IDEA, at least, parsing the types of the returned function.
    – extempl
    Commented Jun 14, 2023 at 5:15
25

You can document the inner function and then reference it like so

/**
 * @param {Number} - number of times to prompt
 * @return {many_prompts~inner} - the returned function
 */
function many_prompts(count){
  /**
   * My inner function
   *
   * @param {object} prompt Some parameter
   */
  var inner = function(prompt){
    for(var i=0;i<count;i++) alert(prompt)
  };
  return inner;
}
7
  • 17
    Is there a way to do this for anonymous inner functions?
    – Jack Allan
    Commented May 20, 2016 at 8:20
  • For JSDoc you would need some form of reference, I guess it depends on how the anonymous function is used. Do you have an example?
    – SGD
    Commented May 20, 2016 at 16:06
  • 8
    Is this documented anywhere officially? Can't find it.
    – Ben Creasy
    Commented Jun 28, 2017 at 3:58
  • 14
    Is there a way to do this if you're using arrow functions? For instance: many_prompts = count => prompt => ...
    – Lucretiel
    Commented Dec 15, 2017 at 2:37
  • 2
    This doesn't seem to be recognized by VS Code. Commented May 17, 2022 at 21:27
24

The way I prefer:

/**
 * @param {number} count - number of times to prompt
 * @returns { (prompt:string) => void } - the returned function
 */
  manyPrompts(count) {
      /**
       * My inner function
       *
       * @param {object} prompt Some parameter
       */
      const inner = function(prompt) {
        for (let i=0; i < count; i++) {
          alert(prompt);
        };
      };
      return inner;
  }
6
  • 2
    Is there any documentation for this? Commented Aug 25, 2020 at 5:57
  • 1
    Yeah, is this standard or non standard? Commented Dec 23, 2020 at 15:16
  • Works in VSCode
    – uadnal
    Commented Mar 4, 2021 at 13:33
  • 4
    promt seems misspelt.
    – Pang
    Commented Apr 8, 2021 at 1:57
  • also works when I use tsc to generate .d.ts files
    – Mz A
    Commented Jul 14, 2022 at 11:10
0

This is my solution for this. I'm not describing a return in the first function and also document the inner function, which results in getting the documentation from the inner function.

/**
 * Function to create a Function with multiple prompt messages
 * @function
 * @param {Number} count - number of times to prompt
 */
function many_prompts(count) {
  /** 
   * To prompt a value multiple times
   * @function
   * @param {String} prompt - parameter to prompt
   * @return {void} prompt the input parameter
   */
  return function(prompt) {
    for(var i=0; i < count; i++) alert(prompt);
  }
}


//Example of use:
var y  = many_prompts(3);
y('Hello World');

Which is then shown e.g. in vscode like this...

For the outer function: description of the outer function in vscode

For the inner function:

description of the inner function in vscode


You can also add an additional description when assigning the function, to describe the difference

/** 
 * Function to prompt a value 3 times
 * @function
 * @param {Number} prompt - parameter to prompt
 * @return {void} prompt the input parameter
 */
const y = many_prompts(3);
y('Hello World');
2
  • 1
    > @return {Function} It never returns Function. Your example returns void.
    – extempl
    Commented Jun 14, 2023 at 4:57
  • @extempl Thanks, that was wrong! I changed it to void.
    – Magraina
    Commented Jun 14, 2023 at 12:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.