Skip to content

Instantly share code, notes, and snippets.

@soswow
Created April 1, 2020 02:15
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 soswow/baad1cf8890a2d963f70685f6bbfe911 to your computer and use it in GitHub Desktop.
Save soswow/baad1cf8890a2d963f70685f6bbfe911 to your computer and use it in GitHub Desktop.
Async generator walk in nodejs
const extractSubNodeModules = async function*(
dir: string,
): AsyncGenerator<string, void, void> {
const files = await readdir(dir);
for (let i = 0; i < files.length; i++) {
const filepath = join(dir, files[i]);
const stats = await stat(filepath);
if (stats.isDirectory()) {
if (files[i] === 'node_modules') {
yield filepath;
} else {
yield* await extractSubNodeModules(filepath);
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment