要求,读取结束后才能输出所有文件

var fs = require('fs');
var path = require('path');

var list = [];
var count = 0;
function readDir(_path, callback) {

var toExec = function (_path) {
count++;
fs.readdir(_path, function (err, files) {
if (err) {
console.log(err);
return;
}
files.forEach(function (file, i) {
var stat = fs.lstatSync(path.join(_path, file));
if (stat.isFile()) {
list.push(path.join(_path, file));
} else if (stat.isDirectory()) {
toExec(path.join(_path, file));
}

if ((i + 1) === files.length) {
count--;
}

if(count === 0){
callback(list);
}
});
});
};

toExec(_path);
};

readDir(path.join(process.cwd(), 'go'), function (_list) {
console.log(_list);
});

相关文章:

  • 2021-10-13
  • 2022-12-23
  • 2021-08-11
  • 2021-11-07
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-07
  • 2022-02-06
  • 2021-12-09
  • 2022-12-23
  • 2022-01-22
  • 2022-12-23
  • 2022-01-27
相关资源
相似解决方案