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() { ... }
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.
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.
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 });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With