How to document resolved values of JavaScript promises

Viewed 9815

Given this code :

function asyncFoo() {
  return new Promise(function (fulfill, reject) {
    doAsyncStuff(function(err, data) {
      if(err) reject(new Error(err));
      else fulfill(new Bar(data));
    });
  });
}

How can I document that asyncFoo will return a Promise that, when fulfilled will yield an instance of Bar, and when rejected will yield an instance of Error?

/**
 * @return << Here, what do I have to write? >>
 */
function asyncFoo() { ... }
3 Answers

I personally prefer strict differentiation - declare final type from async function and Promise from a function that directly returns Promise.

/**
  Async JSON request
  @param {string} sql
  @returns {object}
*/
async function request(sql)
{
  return JSON.parse(await dbEngine.executeRequest(sql));
}

/**
  Async JSON request
  @param {string} sql
  @returns {Promise<string>}
*/
function request(sql)
{
  return dbEngine.executeRequest(sql);
}
Related