I would have the create method accept a callback since doing synchronous
HTTP with node is tricky and discouraged. While you are asynchronously
creating this object (which may indicate a different method name should be
used) you are ultimately returning an object that exposes an API
appropriate for the server version without then having to do a bunch of
conditional code in each exposed method.

-- Daniel R. <[email protected]> [http://danielr.neophi.com/]


On Sat, May 10, 2014 at 10:03 PM, Tony Barbieri <[email protected]> wrote:

> Thanks for the reply Daniel.
>
> Would the create method accept a callback?  Could I and should I perform
> the initial http request to find the server version synchronously?
>
> Thanks again,
>
> Tony
>
>
> On Sat, May 10, 2014 at 3:46 PM, Daniel Rinehart <[email protected]>wrote:
>
>> Have your module expose a create method that makes the initial request to
>> get the version and have that return an object with all of the logic tied
>> to the specific server version to the callback. That way you can avoid
>> needing to check the version in all the subsequent calls.
>>
>> -- Daniel R. <[email protected]> [http://danielr.neophi.com/]
>> 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<https://groups.google.com/d/msgid/nodejs/CAJhmvsQZf1_us0MKV%2BuYPyejA2C-W%2BGBAyO%3DNYUczsHbDG8Fqw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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/CAETDeSBUJnvVfC0ZPRy7bT6HFnc5o_forJ_WmuZfgZ%2BagnxfUQ%40mail.gmail.com<https://groups.google.com/d/msgid/nodejs/CAETDeSBUJnvVfC0ZPRy7bT6HFnc5o_forJ_WmuZfgZ%2BagnxfUQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> -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/CAJhmvsThAuvjemEUuub_BHBJ5xakr6mX1hHJ50VGVwD-cBmvYg%40mail.gmail.com<https://groups.google.com/d/msgid/nodejs/CAJhmvsThAuvjemEUuub_BHBJ5xakr6mX1hHJ50VGVwD-cBmvYg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAETDeSAVNFTSPLGgK_zs%2B3y_k5o0gmHOYfrOCo1udmUbbw9gFQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to