5

For synchronous getter functions, the naming convention is well-defined:

var getFerby = function(){
    ..
    return ferby;
};

However, if the ferby I want is not locally (synchronously) available, a common method is to handle that situation with a callback:

/**
 * Asynchronously gets a ferby and passes it to the callback.
 *  
 *     Once the ferby is retrieved, these rules MUST be followed:
 *       1) Don't feed it after midnight.
 *       2) Don't give it water.
 *       3) Don't let it near bright light.  
 *
 * @param {ferbyCallback} callback - The callback function that expects a ferby.
 */
var fooFerby = function(callback){
    getFerbyLoader().load(function(ferby){
        callback(ferby);
    });
};

/**
 * The callback for the fooFerby function.
 *
 * @callback ferbyCallback
 * @param ferby The ferby
 */

What is a good naming convention for fooFerby so that I know by name that it expects a callback?

4

2 回答 2

5

我使用前缀“fetch”,而不是异步 getter 的“get”。

这个想法是,如果它在本地不可用,则需要获取它。

于 2015-04-15T11:00:19.030 回答
1

.NET 使用 BeginDoAction。我喜欢 JavaScript 中的相同方法。所以在你的情况下,函数是beginGetFerby.

NodeJs 采用大多数方法是异步的约定,同步方法有一个'Sync' 后缀,例如doActionSync。你可以做相反的事情,并有一个“异步”后缀,所以你的函数是getFerbyAsync. 我也喜欢这种方法。

于 2013-11-27T16:17:11.440 回答