18

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() { ... }
1

4 Answers 4

29

Looks like you should do the following, based on some other source code's comments.

/**
 * @return {Promise.<Bar>}
 */

How JavaScript Promises are documented.

Similar question with a similar answer. Note the lack of a dot in that answer.

Sign up to request clarification or add additional context in comments.

3 Comments

Is the dot recommended or not? The JSDoc docs are ironically not very good.
For a bit more information take also a look here: stackoverflow.com/a/44726521/828366
Both jsdoc.app/tags-returns and jsdoc.app/tags-async suggest {Promise<Bar>}
14

I like to specify that it's an async function with @async and specify the fulfilled return with @returns and error with @throws

/**
 * @async
 * @returns {Bar}
 * @throws {Error}
 */
function asyncFoo() { ... }

6 Comments

Should be an accepted answer. Works like a charm. Only by this way result is correctly recognized by IDE when return value is defined as Interface in the TypeScript file (just for neat autocomplete).
and yet, the fn doesn't return {Bar} nor does it throw {Error}
Nor is the function defined as async function Use JSDoc: @async
This should be the way to document it. Arguable a function can be considered async if it can be invoked with the await keyword. The @async annotation is needed for functions not declared with the async keyword but do return a promise.
Documentation suggests this answer is incorrect jsdoc.app/tags-async — the included example shows {Promise<string>}
|
0

There seems to be no way to declare what can be thrown, neither through @ directives nor through the Promise generic parameters (see Typescript Promise rejection type).

Per documentation at https://jsdoc.app/ your code should look like:

/**
 * @returns {Promise<Bar>}
 */
function asyncFoo() {
  return new Promise(function (fulfill, reject) {
    doAsyncStuff(function(err, data) {
      if(err) reject(new Error(err));
      else fulfill(new Bar(data));
    });
  });
}

Comments

-1

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);
}

1 Comment

This is incorrect though. Async functions always return a promise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.