Skip to content

Instantly share code, notes, and snippets.

@ritch
Created November 13, 2012 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritch/4067355 to your computer and use it in GitHub Desktop.
Save ritch/4067355 to your computer and use it in GitHub Desktop.
AwaitScript
// the way you want to do it
var files = [];
function recurse(dir) {
dir.forEach(function (f) {
if(stat(f).isFile()) {
files.push(f);
} else {
recurse(fs.readdir(dir));
}
});
}
try {
recurse(fs.readdir('.'));
} catch(e) {
console.error(e);
}
// the magic way that could maybe work with await
var files = [];
function recurse(dir) {
(await dir).forEach(function (f) {
if((await stat(f)).isFile()) {
files.push(f);
} else {
recurse(fs.readdir(dir));
}
});
}
async try {
recurse(fs.readdir('.'));
} catch(e) {
// same scope, not same tick
console.error(e);
} finally {
console.log('and done...');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment