Skip to content

Instantly share code, notes, and snippets.

@131
Created October 14, 2018 21:26
Show Gist options
  • Save 131/742b43cc71ddf3788125805c080ccb0a to your computer and use it in GitHub Desktop.
Save 131/742b43cc71ddf3788125805c080ccb0a to your computer and use it in GitHub Desktop.
async iterator, are needed
"use strict";
//this requires node 10 magical
const fs = require('fs');
const path = require('path');
const promisify = require('nyks/function/promisify');
const readdir = async function * (dir) {
var entries = await promisify(fs.readdir)(dir);
for(let entry of entries) {
let filepath = path.join(dir, entry);
try {
let stat = fs.statSync(filepath);
if(stat.isDirectory()) {
for await(let subpath of readdir(filepath))
yield subpath;
} else {
yield filepath;
}
} catch(err) {// stat failure
}
}
}
module.exports = readdir;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment