了解JavaScript中的recursionasynchronous调用

这个问题只是为了好奇。 我知道还有其他的方法来做实际的事情。 我只想学习一些asynchronous模式。

我想从根目录开始search特定的文件。 于是我发现了这个文件模块:

https://github.com/mikeal/node-utils/tree/master/file

这个模块提供了一个asynchronous的file.walk(start,callback)函数。 在哪里callback被称为每个find的目录。

想象一下,我希望find具有特定名称的所有文件,将path保存在一个数组中,并稍后对其进行协作处理。

import file = require("file"); var folderArray= []; file.walk(path,function(err,file){ if(doesExist(file+"/mySpecialFileName")){ folderArray.push(file); } }) //when all sub directories are searched do something process(folderArray) 

我怎样才能确保所有的目录都被search,我可以进行处理。 例如在潜水模块: https : //github.com/pvorb/node-dive我只是把我的进程(folderArray)在search完成时调用的第三个callback。

非常感谢。 最好的P