Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

异步文件遍历可能有问题 #35

Open
pamxy opened this issue Sep 10, 2019 · 0 comments
Open

异步文件遍历可能有问题 #35

pamxy opened this issue Sep 10, 2019 · 0 comments

Comments

@pamxy
Copy link

pamxy commented Sep 10, 2019

我照着你的写,但只能编译同一个目录下的一个文件,然后我在想可能没有循环,我认为应该类似这样的:
`function travel(dir,callback,finish){
//异步读取目录
fs.readdir(dir,function(err,files){
//将0传给next,开始递归
for(i=0;i<files.length;++i) {
(function next(i){
//如果当前未完成遍历
if (i<files.length){
//获取到路径文件名
var pathname = path.join(dir, files[i]);
//异步获取此路径文件名的文件状态
fs.stat(pathname,function(err,stats){
//如果此文件是目录
if (stats.isDirectory()){
//再次递归此目录
travel(pathname, callback, function () {
//如果此目录递归完了,继续检查上一层的下一个文件
next(i + 1);
});
}
//此文件是非目录
else {
//执行上层所需的callback函数,完成后继续递归此层的下一个文件
callback(pathname,function(){
next(i + 1);
})
}
})
}
//如果所有文件已经完成遍历
else{
//如果有完成回调函数,执行完成回调函数
finish && finish();
}
}(i))
}
})
}

travel("./",function(pathname){
console.log(pathname);
})`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant