Hello, thanks in advance for your help.

I get the concept of callbacks, and I can see how to use them with the
node api.  But, I'm having trouble fitting them into my own code.
Below is a sample of what I'm talking about.

I'm writing a small api for myself, so that I can use information from
tumblr on my own site.  Here is the line from my app.js file.  This
particular method retrieves the avatar url from tumblr.

//This line would return the host name, I can't figure out how to add
a callback into this.
tumblr.avatar('#some host name', 64);

The next piece of code is out of my tumblr.js file that I created.

exports.avatar = function(hostName, size) {

        var obj;

        if (typeof size === Number) size = 64;

        //create an options for http.get()
        var options = {
          host: 'api.tumblr.com',
          port: 80,
          path: '/v2/blog/' + hostName +
            '.tumblr.com/avatar/' + size
        };

        //retrieve the url of the avatar
        var req = http.get(options, function(res) {
          var data = '';

          res.on('data', function(chunk) {
                  data += chunk;
          });

          res.on('end', function(err) {
                  obj = JSON.parse(data);
                  obj = obj.response.avatar_url;
          });
        });
};


I already have the call back within the http.get() method, but I can't
figure out where to put a call back into my avatar function, to return
the URL of the avatar back to the app.js file.

So my question is how would I go about adding a callback to this
function, so that I don't block node?

A point in the right direction would be much appreciated.

Thanks.

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to