koen
koen

Reputation: 1057

Node.js event driven paradigm = messy code?

I am coming from a PHP background and am now trying to get used to the event driven paradigm of Node.js. However, my code quickly get messy. Below I compare procedural code with actual Node.js Redis code. Am I doing this right?

PROCEDURAL (pseude code)

if(!client.get("user:name:koen")) {
    client.set("user:name:koen", "user:id:" + client.incr("count:users"));
}

EVENT DRIVEN (actual code)

client.get("user:name:koen", function(err, res) {
  if(!res){
    client.incr("count:users", function(err, count){ 
      client.set("user:name:koen", "user:id:" + count, function (err, res) {
        callback(err, res);
      });
    }); 
  }
});

Upvotes: 2

Views: 1156

Answers (1)

Andrei Karpuszonak
Andrei Karpuszonak

Reputation: 9034

Callback hell, mentioned in the question, is greately explained here, as well as how to write the code to avoid it:

http://callbackhell.com/

Upvotes: 1

Related Questions