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

前端 未结 6 1972
隐瞒了意图╮
隐瞒了意图╮ 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:40

    Even if they don't exist in Javascript, I found that JSdoc understands "generic types".

    So you can define your custom types and then use /* @return Promise */. The following result in a nice TokenConsume(token) → {Promise.} with a link to your custom Token type in the doc.

    /**
     * @typedef Token
     * @property {bool} valid True if the token is valid.
     * @property {string} id The user id bound to the token.
     */
    
    /**
     * Consume a token
     * @param  {string} token [description]
     * @return {Promise} A promise to the token.
     */
    TokenConsume = function (string) {
      // bla bla
    }
    

    It even works with /* @return Promise */ or /* @return Promise */.

提交回复
热议问题