Hello!

I am pretty new to nodejs and I'm writing a client api module that will
provide CRUD operations by calling json-rpc methods on an outside server.
 The server provides it's currently running version via a json-rpc request
and based on that the client api may or may not have additional
functionality.

My question is more of a design question than anything else.  I'm not sure
the best way to get the server's version and cache it within an
asynchronous environment.  I am currently using the http module to make the
requests and the q module to use promises.  Below is some pseudo code to
illustrate basically what I have.


function Client(url, opts) {
>     var info = {
>         'url': url,
>         'server_version': null
>     };
> };
>


Client.prototype.server_version = function (cb) {
>     if (!this.info.server_version){
>         this.request("info").then(function(result) {
>             this.info.server_version = result.version;
>         }).nodeify(cb);
>     } else {
>         cb(this.info.server_version);
>     };
> };
>


Client.prototype.read = function(params, cb) {
>     this.server_version(function(err, result) {
>         if (result > [3, 3, 0]) {
>             ...
>         };
>         else {
>             ...
>         }
>     });
>     return this.request(params).nodeify(cb);
> };
>


Client.prototype.request = function(method) {
>     defer = Q.defer();
>     ...
>     return defer.promise;
> };



I guess my question is this the "right" way to deal with figuring out the
server version to make logical decisions at various points in the code?  I
see a few issues with the way I am currently doing it, if the
server_version is requested multiple times before it's been cached there
will be unnecessary additional calls made to the server.  It would be nice
to have a better way to attach callbacks to the original promise that's
made when the first request is sent out.  Also using always using a
callback and nested functions to check the server version feels a bit
inelegant.  I'm not sure if this is the way it needs to be done.  Is this a
situation where I would want to use yield?  If so do I need to run the
unstable 11.* version of Node to take advantage of that?

I apologize in advance if this question is vague.  I am trying to
understand how to do something asynchronous that I would normally do
synchronously, ie: halting code flow the first time a call to check the
server_version is made and being able to reference
"this.info.server_version" afterwards versus always having to use a
callback to do the check.

Any and all advice is much appreciated.

Best,

Tony

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAJhmvsQZf1_us0MKV%2BuYPyejA2C-W%2BGBAyO%3DNYUczsHbDG8Fqw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to