循环中的node.jsasynchronous函数?

我有一些node.js的问题。 我想要做的是获取“./"+req.user.email中的目录数组,并通过循环find它们的大小并添加一个表格行来输出,你可以在代码中看到。 最后,我不想使用res.send()发送所有的表行。

但是,我得到的唯一的输出是:

<tr></tr> 

为数组中的每个文件。 看起来,forEach函数并不等待readSizeRecursive。 readSizeRecursive函数是asynchronous的,我相信这是什么导致的问题,但我不知道我怎么能解决这个问题。

任何帮助将不胜感激,我也包括readSizeRecursive函数。 谢谢!

  var output = ""; fs.readdir("./" + req.user.email, function (err, files) { files.forEach(function(file){ output += "<tr>"; readSizeRecursive("./"+req.user.email+"/"+file, function (err, total){ output += '<td>' + file + '</td><td>' + total + '</td>'; }); output += "</tr>" }); res.send(output) }); 

readSizeRecursive():

 // Function to find the size of a directory function readSizeRecursive(item, cb) { fs.lstat(item, function(err, stats) { var total = stats.size; if (!err && stats.isDirectory()) { fs.readdir(item, function(err, list) { async.forEach( list, function(diritem, callback) { readSizeRecursive(path.join(item, diritem), function(err, size) { total += size; callback(err); }); }, function(err) { cb(err, total); } ); }); } else { cb(err, total); } }); } 

 var fs = require ("fs"); var createTableContent = function (p, cb){ var read = function (p, cb){ //Prevent recursion if error if (err) return cb (); fs.stat (p, function (error, stats){ if (error){ err = error; return cb (); } if (stats.isDirectory ()){ var dirSize = 0; fs.readdir (p, function (error, entries){ if (error){ err = error; return cb (); } var pending = entries.length; //Empty dir if (!pending) return cb (0); entries.forEach (function (entry){ read (p + "/" + entry, function (entrySize){ dirSize += entrySize; if (!--pending) return cb (dirSize); }); }); }); }else{ cb (stats.size); } }); }; //A lot of errors can be produced, return only the first one var err = null; //Suppose p is a dir fs.readdir (p, function (error, entries){ if (error) return cb (error); var content = ""; var pending = entries.length; if (!pending) return cb (null, content); entries.forEach (function (entry){ read (p + "/" + entry, function (totalSize){ if (err) return cb (err); content += "<tr><td>" + entry + "</td><td>" + totalSize + "</td></tr>"; if (!--pending){ //End cb (null, content); } }); }); }); }; //Here goes the "email" path createTableContent (".", function (error, content){ if (error) return console.error (error); console.log (content); }); 

请使用这种模式的asynchronous模块。 使用async.each将允许您asynchronous计算每个文件夹的大小,然后在完成单独计算后再返回大小。

 var output = []; fs.readdir('./' + req.user.email, function (err, files) { async.each(compute, report); }); function compute (file, done) { // calculate size, then callback to signal completion // produce a result like below, then invoke done() var obj = { files: [ { name: file, size: size }, { name: file, size: size }, { name: file, size: size } ]}; output.push(obj); done(); } // doesn't need to be this awful function format (list) { var result = []; list.forEach(function (item) { var description = item.files.map(function (file) { return util.format('<td>%s</td><td>%s</td>', file.name, file.size); }); result.push(description); }); result.unshift('<tr>'); result.push('</tr>'); return result.join('</tr><tr>'); } function report (err) { if (err) { return next(err); } var result = format(output); res.send(result); } 

这样,您就可以轻松地更换不同的function块,例如,在不改变文件大小树的计算的情况下更改格式。

你的主要问题是控制stream。 您在res.send返回时,asynchronous循环并计算大小。