简体   繁体   English

异步getter的命名约定

[英]Naming convention for asynchronous getter

For synchronous getter functions, the naming convention is well-defined: 对于同步getter函数,命名约定是明确定义的:

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: 但是,如果我想要的ferby不是本地(同步)可用的,那么常见的方法是使用回调来处理这种情况:

/**
 * 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? 什么是fooFerby的良好命名约定,以便我知道它希望回调?

I use the prefix "fetch", instead of "get" for asynchronous getters. 我使用前缀“fetch”,而不是异步getter的“get”。

The idea is that if it is not locally available, you need to fetch it. 这个想法是,如果它不是本地可用的,你需要获取它。

.NET uses BeginDoAction. .NET使用BeginDoAction。 I like the same approach in JavaScript. 我喜欢JavaScript中的相同方法。 So in your case, the function would be beginGetFerby . 所以在你的情况下,函数将是beginGetFerby

NodeJs takes the convention that most methods are asynchronous, and the synchronous methods have a 'Sync' suffix, eg doActionSync. NodeJs采用大多数方法都是异步的约定,同步方法有一个“Sync”后缀,例如doActionSync。 You could do the opposite, and have an 'Async' suffix, so your function would be getFerbyAsync . 你可以做相反的事情,并有一个'Async'后缀,所以你的函数将是getFerbyAsync I like that approach too. 我也喜欢这种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM