Skip to content

Instantly share code, notes, and snippets.

@Ianfeather
Created March 17, 2015 14:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ianfeather/fefaee82c2ac0e66fddd to your computer and use it in GitHub Desktop.
Save Ianfeather/fefaee82c2ac0e66fddd to your computer and use it in GitHub Desktop.
List files using node and generators
'use strict';
var co = require('co'),
fs = require('co-fs'),
each = require('co-each');
var dive = function* (path) {
var items = yield fs.readdir(path);
yield each(items, function* (item) {
var subPath = `${path}/${item}`,
stat = yield fs.lstat(subPath);
if (stat.isDirectory()) {
yield dive(subPath)
} else {
console.log(subPath)
}
});
}
co(function* () {
yield dive(process.cwd())
});
@Ianfeather
Copy link
Author

How the same could look using async/await h/t @rob-mccann

async function* ls(path) {
    let files = await fs.readdir(path);

    for (let file of files) {
        let subPath = `${path}/${file}`,
            stat = await fs.lstat(subPath);

        if (stat.isDirectory()) {
            yield* ls(subPath);
        } else {
            yield subPath;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment