节点asynchronous瀑布callback已被调用

我试图读取从asir.waterfall目录的一些文件,在我看来,我正在做的东西正确的,但我得到了指定的错误和readData函数从来没有被调用。 怎么了?

var fs = require("fs"); var async = require("async"); var folder = "./files/"; try { async.waterfall([ function readDir(cb) { fs.readdir(folder, function(err, files) { cb(err, files); }); }, function loopFiles(files, cb) { files.forEach(function(fn) { console.log("loop " + fn); cb(null, fn); }); }, function check(fn, cb) { console.log("check "+fn); fs.stat(folder + fn, function(err, stats) { console.log(stats.isFile()); cb(err, stats, fn); }); }, function readData(stats, fn, cb) { console.log("read "+fn); if (stats.isFile()) { fs.readFile(folder + fn, "utf-8", function(err, data) { cb(err, data); }); } } ], function(err, result) { if (err) { throw err; } console.log(result); }); } catch (err) { console.log(err); } 

问题是,如果files.length > 1多次调用loopFiles() cb(null, fn) 。 您可能需要为每个文件执行一个单独的async.waterfall()或使用其他async.*方法。

另一个问题是在readData() ,在stats.isFile()计算结果为false的情况下,您不调用cb()