How to specify resolution and rejection type of the promise in JSDoc?

前端 未结 6 1979
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 05:56

I have some code that returns a promise object, e.g. using Q library for NodeJS.

var Q = require(\'q\');

/**
 * @returns          


        
6条回答
  •  逝去的感伤
    2021-01-30 06:38

    With JSDoc you can also create custom types using @typedef. I use this quite a bit so props/params that are strings or arrays link to a description of the type (like for string I created a typedef that includes the natives available to string (see the example JSDoc below). You can define a custom type in this same manner. This is because you can't use the object dot notation for returns like you can for @property to denote what is in the return. So in cases where you are returning something like an object, you can create a definition for that type ('@typedef MyObject) and then @returns {myObject} Definition of myObject.

    I wouldn't go crazy with this, though, because types should be as literal as possible and you don't want to pollute your types, but there are cases where you want to define the type explicitly, and so you can document what is in it (a good example is Modernizr...it returns an object, but you have no documentation of it, so create a custom typedef that details what is in that return).

    If you don't need to go that route, then as someone already mentioned, you can specify multiple types for any @param, @property, or @return by using the pipe |.

    In your case, you should also document an @throws because you are throwing a new error: * @throws {error} Throws a true new error event when the property err is undefined or not available.

    //saved in a file named typedefs.jsdoc, that is in your jsdoc crawl path
    /**
        * @typedef string
        * @author me
        * @description A string literal takes form in a sequence of any valid characters. The `string` type is not the same as `string object`.
        * @property {number} length The length of the string
        * @property {number} indexOf The occurence (number of characters in from the start of the string) where a specifc character occurs
        * @property {number} lastIndexOf The last occurence (number of characters in from the end of the string) where a specifc character occurs
        * @property {string|number} charAt Gives the character that occurs in a specific part of the string
        * @property {array} split Allows a string to be split on characters, such as `myString.split(' ')` will split the string into an array on blank spaces
        * @property {string} toLowerCase Transfer a string to be all lower case
        * @property {string} toUpperCase Transfer a string to be all upper case
        * @property {string} substring Used to take a part of a string from a given range, such as `myString.substring(0,5)` will return the first 6 characters
        * @property {string} substr Simialr to `substring`, `substr` uses a starting point, and then the number of characters to continue the range. `mystring.substr(2,8)` will return the characters starting at character 2 and conitnuing on for 8 more characters
        * @example var myString = 'this is my string, there are many like it but this one is HOT!';
        * @example
        //This example uses the string object to create a string...this is almost never needed
        myString = new String('my string');
        myEasierString = 'my string';//exactly the same as what the line above is doing
    */
    

提交回复
热议问题