Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About Node's code style

EDIT

thx to all the answers,
and finally I decide to use some tools like Step,

all I need is "flow control" and don't want any thing else which may slow down the performance (I don't know how much exactly it would effect or the effect just can be ignored).

So I just create a little tool for flow control:

line.js

/**
 * Create the "next" function
 * 
 * @param {Array} tasks 
 * @param {Number} index
 * @param {Number} last
 */
var next = function(tasks, index, last) {

    if (index == last) {

        return tasks[index + 1];
    }
    else {

        return function(data) {

            var nextIndex = index + 1;
            tasks[nextIndex](next(tasks, nextIndex, last), data);
        };
    }
};

/**
 * Invoke functions in line.
 */
module.exports = function() {

    var tasks = arguments,
        last = tasks.length - 2;

    tasks[0](next(tasks, 0, last));
};

usage:

var line = require("line.js");
line(function(next) {

    someObj.find(function(err, docs) {

        // codes
        next(docs);
    });
}, function(next, docs) {

    // codes
});

Hope this helps.

EDIT END


As all know,
Node's built-in or third-part modules often provides async API,
and using "callback" function for dealing the results.

It's cool but sometimes would code like this:

                   //some codes
              }
         }
    }
}

codes like this are hard to read.

I know "deferred" library can solve such problem,
Is there any good "deferred" module for Node?
And How is the performance if I code Node with "deferred"?

like image 750
Kevin Avatar asked Jun 10 '26 04:06

Kevin


1 Answers

It is a large problem with Node-based code; you frequently grow "callback pyramids". There are several approaches to dealing with the problem:

Code style:

Use this annoyance as an opportunity to break your code into bite sized chunks. It means you're likely going to have a proliferation of tiny named funcs - that's probably just fine, though! You might also find more opportunities for reuse.

Flow-control Libraries

There are exactly 593.72 billion flow control libraries out there. Here's some of the more popular ones:

  • Step super basic serial & parallel flow management.
  • seq is a heavier but more feature-full flow control library.
  • There's plenty more. Search the npm registry for "flow" and "flow control" (sorry, doesn't appear to be linkable)

Language Extensions

There are several attempts to provide a more synchronous-feeling syntax on top of JavaScript (or CoffeeScript), often based on the concepts behind the tame paper.

  • TameJS is the OkCupid team's answer to this.
  • IcedCoffeeScript they've also ported TameJS over CoffeeScript as a fork.
  • streamline.js is very similar to TameJS.
  • StratifiedJS is a heavier approach to the problem.

This route is a deal-breaker for some:

  • It's not standard JavaScript; if you are building libraries/frameworks/etc, finding help will be more difficult.
  • Variable scope can behave in unexpected ways, depending on the library.
  • The generated code can be difficult to debug & match to the original source.

The Future:

The node core team is very aware of the problem, and are also working on lower level components to help ease the pain. It looks like they'll be introducing a basic version of domains in v0.8, which provide a way of rolling up error handling (avoiding the common return err if err pattern, primarily).

This should start to lay a great foundation for cleaner flow control libraries, and start to pave the way for a more consistent way of dealing with callback pyramids. There's too much choice out there right now, and the community isn't close to agreeing on even a handful of standards yet.

References:

  • Mixu's Node book has an awesome chapter on this subject.
like image 174
Nevir Avatar answered Jun 11 '26 17:06

Nevir