Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention for asynchronous getter

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?

like image 810
Briguy37 Avatar asked Jun 11 '13 14:06

Briguy37


2 Answers

I use the prefix "fetch", instead of "get" for asynchronous getters.

The idea is that if it is not locally available, you need to fetch it.

like image 124
Jean Vincent Avatar answered Nov 02 '22 11:11

Jean Vincent


.NET uses BeginDoAction. I like the same approach in JavaScript. So in your case, the function would be beginGetFerby.

NodeJs takes the convention that most methods are asynchronous, and the synchronous methods have a 'Sync' suffix, e.g. doActionSync. You could do the opposite, and have an 'Async' suffix, so your function would be getFerbyAsync. I like that approach too.

like image 40
Alex Avatar answered Nov 02 '22 11:11

Alex