Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document resolved values of JavaScript promises

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() { ... }
like image 748
DarkChipolata Avatar asked Mar 08 '16 13:03

DarkChipolata


People also ask

What happens when a promise is resolved?

A promise is said to be settled if it is not pending, i.e. if it is either fulfilled or rejected. A promise is resolved if it is settled or if it has been “locked in” to match the state of another promise. Attempting to resolve or reject a resolved promise has no effect.

How do you use promise resolve value?

Promise resolve() method:If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.

How do you get a value returned by a promise?

Accessing the value of promiseB is done in the same way we accessed the result of promiseA . promiseB. then(function(result) { // here you can use the result of promiseB });


1 Answers

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.

like image 56
Ethan B Martin Avatar answered Oct 03 '22 14:10

Ethan B Martin