//找到html目录下的所有目录,并打印出来 var dirArray = new Array(); fs.readdir("html", (err, files) => { if (err) console.log(err); //选择递归调用来解决 (function getdir(i){ if(i === files.length){ console.log(dirArray); return; } fs.stat("html/"+files[i],(error,stats)=>{ if(error){console.log(error);return;} //下面一句让我搞不懂 if(stats.isDirectory()) {dirArray.push(files[i]);console.log(files[i])} }) getdir(i+1); })(0) }) // 最终结果: // [](预想的结果是['bb','upload']) // bb // upload
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4798/viewspace-3139652/ ,如需转载,请注明出处,否则将追究法律责任。