1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| const path = require('path'); const fs = require('fs').promises;
async function done (paths, root) { const files = await fs.readdir(root);
const endDirs = []; const endFiles = [];
for (const file of files) { const filePath = path.join(root, file); const state = await fs.stat(filePath); if (state.isDirectory()) { paths.push(filePath); endDirs.push(file); } else { endFiles.push(file); } }
return [root, endFiles, endDirs]; }
function * walk (root) { const paths = [root];
for (let index = 0; ;index++) { if (paths[index] === undefined) { break; } yield done(paths, paths[index]); } }
async function main () { let index = 0; for (const p of walk('/Users/maple/Desktop')) { const result = await p; console.log(result); index++; if (index > 4) { break; } } }
main();
|