Posts

Showing posts from December, 2010

Asynchronous directory tree walk in node.js

I wrote an asynchronous directory tree walker in node.js . Note the use of Continuation Passing Style in the fileCb callback. That allows the callback to perform its own asynchronous operations before continuing the directory walk. (This code is provided under the Apache Licence 2.0 .) // asynchronous tree walk // root - root path // fileCb - callback function (file, next) called for each file // -- the callback must call next(falsey) to continue the iteration, // or next(truthy) to abort the iteration. // doneCb - callback function (err) called when iteration is finished // or an error occurs. // // example: // // forAllFiles('~/', // function (file, next) { sys.log(file); next(); }, // function (err) { sys.log("done: " + err); }); function forAllFiles ( root , fileCb , doneCb ) {      fs . readdir ( root , function processDir ( err , files ) {          if ( err ) {              fileCb ( err );          } else {              if ( files .