tree-traversal.js
function traverseRecursive(rootNode, options) {
  options = extend({
    subnodesAccessor: function(node) { return node.subnodes; },
    onNode: function(node, callback, userdata) {},
    onComplete: function(rootNode) {},
    userdata: null
  }, options);

  (function visitNode(node, callback) {
    var subnodes = options.subnodesAccessor(node);
    async.eachSeries(subnodes, function(subnode, next) {
      visitNode(subnode, function() {
        async.setImmediate(next);
      });
    },
    function() {
      options.onNode(node, function() {
        async.setImmediate(callback);
      }, options.userdata);
    });
  })(rootNode, function() {
    options.onComplete(rootNode);
  });
}
function traverseRecursive(rootNode, options) {
  options = extend({
    subnodesAccessor: function(node) { return node.subnodes; },
    onNode: function(node, callback, userdata) {},
    onComplete: function(rootNode) {},
    userdata: null
  }, options);

  (function visitNode(node, callback) {
    var subnodes = options.subnodesAccessor(node);
    async.eachSeries(subnodes, function(subnode, next) {
      visitNode(subnode, function() {
        async.setImmediate(next);
      });
    },
    function() {
      options.onNode(node, function() {
        async.setImmediate(callback);
      }, options.userdata);
    });
  })(rootNode, function() {
    options.onComplete(rootNode);
  });
}
function traverseRecursive ( rootNode , options ) { options = extend ( { subnodesAccessor : function ( node ) { return node . subnodes ; } , onNode : function ( node , callback , userdata ) { } , onComplete : function ( rootNode ) { } , userdata : null } , options ) ; ( function visitNode ( node , callback ) { var subnodes = options . subnodesAccessor ( node ) ; async . eachSeries ( subnodes , function ( subnode , next ) { visitNode ( subnode , function ( ) { async . setImmediate ( next ) ; } ) ; } , function ( ) { options . onNode ( node , function ( ) { async . setImmediate ( callback ) ; } , options . userdata ) ; } ) ; } ) ( rootNode , function ( ) { options . onComplete ( rootNode ) ; } ) ; }
Asynchronously traverses the tree recursively.
Asynchronously traverses the tree recursively .