How should I write simple sequential GETs?

Node.js and redis look hot but I'm getting burned. I can't gracefully overcome what seems like a very simple task:

Given 3 numbers, check if the first two are greater than 10. If yes, print the third number.

I did accomplish this task with this rather comical script:

var http = require("http");
var redis = require("redis");
client = redis.createClient();

http.createServer(function(request, response) {
    client.SET("key1", "11");
    client.SET("key2", "9");
    client.SET("key3", "3");

    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello\n\n");
    client.GET("key1", function(err, reply) {
        response.write(reply + "\n\n");

        if(parseInt(reply) > 10) {
          client.GET("key2", function(err, reply) {
              response.write(reply + "\n\n");

              if(parseInt(reply) > 10) {
                client.GET("key3", function(err, reply) {
                    response.write(reply + "\n\n");

                    response.end();
                    client.end();
                  });
              } else {
                response.end();
                client.end();
              }

            });
        } else {
          response.end();
          client.end();
        }
      });
  }).listen(8000);

The few things I noticed are:

  • is it necessary to nest each time I want a new GET?
  • response.end() needs to called somewhere for the writes to appear. How do I avoid repeating them in the else blocks?

Then I tried this style of querying the DB:

http.createServer(function(request, response) {

client.SET("key1", "11");
client.SET("key2", "9");
client.SET("key3", "3");

var key1 = 0;
var key2 = 0;
var key3 = 0;

response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello\n\n");
client.GET("key1", function(err, reply) {
        response.write(reply + "\n\n");
        key1 = parseInt(reply);
    });

client.GET("key2", function(err, reply) {
        response.write(reply + "\n\n");
        key2 = parseInt(reply);
    });

client.GET("key3", function(err, reply) {
        response.write(reply + "\n\n");
        key3 = parseInt(reply);
    });

response.write(key1 + "\n\n");
response.write(key2 + "\n\n");
response.write(key3 + "\n\n");

response.end();
client.end();

After some reading and some thought, my explanation is that the code gets to the response.writes before the asynchronous GETs return, and then the response is closed so the inner writes never happen. At least I noticed from the first example that the SETs can be called easily, but I don't know (and it doesn't seem to matter) if they are actually set in that order.

So, perhaps I'm doing something that is not suitable for node and redis. And I must be missing something basic and obvious. What do I need to do to level up?

Or tell me I have to go back to PhpMyAdmin :(

Have a look at async it can help you organize your code https://github.com/caolan/async Hope this helps