0

I'm learning Node.js by converting an existing Rails project. It's taken some time to get my head around callbacks and the async nature of Node.

The first general attempt looked ok, my RoR functionality was replicated and general speed was much faster using Node.

Having watched the nodecast here I started wondering whether my creation went against all the Node principals.

I have some function which, depending on the results call other functions. Simple, if, else if, else logic.

What I'm trying to figure out is if it's OK to do this or whether I should using something like the async package.

One particular function looks like this:

  checkAuthorization: function(socket,callback) {
    client.hget(socket.mac, 'authorized', function(err, val) {
      callback(val);
      if (val == null) {
        checkExteralAuth(socket.mac, socket.serial, function(val) {
          data = JSON.parse(val)
          authorized = (data["live"] == 'yep') ? true : false
          setRedisHash(socket.mac, 'authorized', authorized);
        });
      };
    });
  }

Is there a mode "node" way of doing this?

2
  • It's definitely ok. However it's very nice (from a readability/maintainability perspective) to use async. In the end it's just a matter of personal preference whether or not you should use async or other similar libraries. That also means that this question is not a good fit for SO since any answer will be based on opinions (edit: or an answer will be "yes"). Jun 27, 2013 at 11:01
  • Thanks, appreciated with was going to provoke opinions but needed some general guidance. Thought I'd take my chance to learn something new. Jun 27, 2013 at 11:10

2 Answers 2

2

Why wouldn't it be? Node.js is a JavaScript platform. It's perfectly acceptable to call functions inside functions in a functional programming language. You do it all the time.

That said, sometimes you might want to avoid simply calling the function, especially with callbacks. The way you're calling the function, referenced by callback, implies that unless that function was explicitly bound to some context (using bind), you're losing the context.

Instead, callback.apply(this, [val]); might be the preferred way of going about your business.

Another thing is that since you're passing a callback function to be called later on, you might want to seriously look into using async as much as possible. I don't have an awful lot of experience with node.js, but by its very nature you'll find yourself writing callbacks, handlers and just using general async trickery all over the place.

Having a package that makes that job a lot easier is always handy...

11
  • apply & call are slowwww Jun 27, 2013 at 11:04
  • @GabrielLlamas: They're twice, to 4 times slower, yes, but calling the function directly does mean you lose the call-context, which you might need => if speed matters, and the context is invariable, use bind... Jun 27, 2013 at 11:10
  • 2
    Call is around 40% slower, Apply around 80% in recent versions of chrome, bind is nearly 100% slower... so at least for now the use of bind isn't the smartest choice when speed is an issue: jsperf.com/function-calls-direct-vs-apply-vs-call-vs-bind/8 Jun 27, 2013 at 11:12
  • 1
    @DavidMulder: JS is neigh on impossible to benchmark, and the speed differences between various engines is huge. Also, the relentless speed-battle between FF and Chrome means that the speed differences we see today might not apply the day after. A while back, someone tested this, and apply even outperformed a direct call in chrome back then... JS engines optimize and JIT-compile, so it all depends, really. Jun 27, 2013 at 11:15
  • 1
    Don't you worry, your question has just received the golden 'accept' :) Was just sidetracked into reading about async. Thanks again. Jun 27, 2013 at 12:10
1

There isn't anything fundamentally un-node-ish in this code, most libraries you will see are written exactly like this.

However, when you're working on an application, which tends to grow rather quickly, you eventually run into what is called the "callback hell", where the level of nesting reaches a level where you can have dozens of nested anonymous function calls.

This quickly becomes unmaintainable, which is where control flow libraries come in. There are several of the, the most popular ones being async and q.

async can be dropped readily into any codebase to manage callback hell, q requires architecting your code to work as Promises, but results in generally nicer API.

1
  • Ok, thanks. I'll take a look at q as well. Think I might continue into callback hell and then see just how bad things are. Good to understand such things I think. Jun 27, 2013 at 11:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.