JavaScript has the asynchronous nature, and CroJSDoc provides some tags for documentation asynchronicity.
Use @nodejscallback.
// (Borrowed from Node.js Manual)
/**
* Asynchronously reads the entire contents of a file.
* @param {String} filename
* @param {Object} [options]
* @param {String} [options.encoding=null]
* @param {String} [options.flag='r']
* @return {String}
* @nodejscallback
*/
function readFile(filename, options, callback) {
require('fs').readFile(filename, options, callback);
}
readFile('test', { encoding: 'utf-8' }, function (error, content) {
console.log(content);
});
# (Borrowed from Node.js Manual)
##
# Asynchronously reads the entire contents of a file.
# @param {String} filename
# @param {Object} [options]
# @param {String} [options.encoding=null]
# @param {String} [options.flag='r']
# @return {String}
# @nodejscallback
readFile = (filename, options, callback) ->
require('fs').readFile filename, options, callback
readFile 'test', encoding: 'utf-8', (error, content) ->
console.log content
The result of the above comment:
Asynchronously reads the entire contents of a file.
Name | Type | Description |
---|---|---|
filename | String | |
options optional | Object | |
encoding=null optional | String | |
flag='r' optional | String | |
callback | Function(error, result) | NodeJS style's callback |
error | Error | See throws |
result | String | See returns |
Name | Type | Description |
---|---|---|
(Returns) | String |
Use @promise.
/**
* Asynchronously reads the entire contents of a file.
* @param {String} filename
* @param {Object} [options]
* @param {String} [options.encoding=null]
* @param {String} [options.flag='r']
* @return {String}
* @promise
*/
function readFile(filename, options) {
return Promise.promisify(require('fs').readFile)(filename, options);
}
readFile('test', { encoding: 'utf-8' })
.then(function (content) {
console.log(content);
});
##
# Asynchronously reads the entire contents of a file.
# @param {String} filename
# @param {Object} [options]
# @param {String} [options.encoding=null]
# @param {String} [options.flag='r']
# @return {String}
# @promise
readFile = (filename, options) ->
Promise.promisify(require('fs').readFile) filename, options
readFile 'test', encoding: 'utf-8'
.then (content) ->
console.log content
The result of the above comment:
Asynchronously reads the entire contents of a file.
Name | Type | Description |
---|---|---|
filename | String | |
options optional | Object | |
encoding=null optional | String | |
flag='r' optional | String |
Name | Type | Description |
---|---|---|
(Returns) | String |
@nodejscallback and @promise can be used together.
/**
* Asynchronously reads the entire contents of a file.
* @param {String} filename
* @param {Object} [options]
* @param {String} [options.encoding=null]
* @param {String} [options.flag='r']
* @return {String}
* @nodejscallback
* @promise
*/
function readFile(filename, options, callback) {
return Promise.promisify(require('fs').readFile)(filename, options)
.nodeify(callback);
}
readFile('test', { encoding: 'utf-8' }, function (error, content) {
console.log(content);
});
// or
readFile('test', { encoding: 'utf-8' })
.then(function (content) {
console.log(content);
});
##
# Asynchronously reads the entire contents of a file.
# @param {String} filename
# @param {Object} [options]
# @param {String} [options.encoding=null]
# @param {String} [options.flag='r']
# @return {String}
# @nodejscallback
# @promise
readFile = (filename, options, callback) ->
Promise.promisify(require('fs').readFile) filename, options
.nodeify callback
readFile 'test', encoding: 'utf-8', (error, content) ->
console.log content
# or
readFile 'test', encoding: 'utf-8'
.then (content) ->
console.log content
The result of the above comment:
Asynchronously reads the entire contents of a file.
Name | Type | Description |
---|---|---|
filename | String | |
options optional | Object | |
encoding=null optional | String | |
flag='r' optional | String | |
callback optional | Function(error, result) | NodeJS style's callback |
error | Error | See throws |
result | String | See returns |
Name | Type | Description |
---|---|---|
(Returns) | String |
If you don't use NodeJS style's callback or the function returns an other value immediately, you can use ordinary tags.
// (Borrowed from CORMO)
/**
* Finds a record by id.
*
* If a callback is given, it returns the record in the callback.
* Otherwise, it returns an Query object for chaining.
* @param {RecordID} id
* @param {Function} [callback]
* @param {Error} callback.error
* @param {Model} callback.record
* @return {Query}
*/
find = function (id, callback) {}
# (Borrowed from CORMO)
##
# Finds a record by id.
#
# If a callback is given, it returns the record in the callback.
# Otherwise, it returns an Query object for chaining.
# @param {RecordID} id
# @param {Function} [callback]
# @param {Error} callback.error
# @param {Model} callback.record
# @return {Query}
@find: (id, callback) ->
The result of the above comment: