Take the 2-minute tour ×

I've just started using Step, and I'm trying to get the stat information of all files in a directory.

However as I'm calling fs.stat in the second step, I still need the full path. How can I pass it to the next method? I've tried this(directory) but it didn't work as I expected.

var getFiles = step.fn(
    function readDir(directory) {
        var p = path.join(__dirname, directory);
        fs.readdir(p, this); // *** How do I pass 'directory' to the next method?

    function readFiles(err, results, directory) {
        if (err) throw err;
        // Create a new group
        var group = this.group();
        results.forEach(function (filename) {
            var p = path.join(__dirname, directory, filename);
            // fs.stat requires a full path
            fs.stat(p, group()); // Could be this.parallel() ??

// later...
var files = getFiles('data');

As I understand it, readDir gets called once, then readFiles gets called, but all in series as fs.readdir's callback just gets called once, with an array of files.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You could use a variable scoped outside getFiles (hacky), or you could also use a closure.

Personally, I would switch from step to async (https://github.com/caolan/async). The waterfall method provided in async is what you're really looking for. Async has the same functionality as step and more.

share|improve this answer
I ended up swapping to Async. It's the bomb. –  Ben Humphreys Oct 4 '11 at 3:34
I disagree completely. Outside scoped variables is not a bad thing, but needs to be done correctly. Scope lookups are expensive; but by creating a local reference to the scoped variable there's really no problem. Regarding switching to async: In my experience, with Step you may express something more elegantly than in async. Not perfect, though, and a library combining their ideas and functionalities would be nice. The big truth is, that though single-threaded environments make a lot of things easier, I still wouldn't call it easy in total. –  Sune Rasmussen Jun 22 '12 at 11:36
add comment
fs.readdir(p, (function(err, files) {
  this(err, files, directory);

this is just a function. You can call it explicitly.

share|improve this answer
add comment

You could always use Q-Oper8:


Then you could safely use the sync fs methods which might make it easier to do what you're wanting to do.

share|improve this answer
add comment

Your Answer


