Node js Promises具有递归函数

时间:2017-07-28 18:49:04

标签: node.js recursion promise

我想从特定目录中读取所有(文本)文件,并且所有子文件都是递归的。我能够读取文件并将结果附加到全局变量。但我想在所有操作结束时访问变量。我正在尝试承诺,但我无法访问它。请帮忙



var file_path = `C:\\Users\\HP\\Desktop\\test_folder`;
const fs = require('fs');
var final_array = [];

let getFolderTree = function(file_path) {
  return new Promise(function(resolve, reject) {

    fs.readdir(file_path, function(err, folders) {
      if (err) {

        console.log("error reading folder :: " + err);

      } else {

        if (folders.length !== 0) {

          for (let i = 0; i < folders.length; i++) {

            if (folders[i].endsWith("txt")) {

              let text_file_path = file_path + `\\` + folders[i];
   
              fs.readFile(text_file_path, function(error_read, data) {
                if (error_read) {
                  console.log("error reading " + error_read);
                } else {
                  
                  return resolve(final_array.push(data));// want to access final_array at the end of all operations
                }
              });
              
            } else {
              let current_path = file_path + `\\` + folders[i];
              getFolderTree(current_path);
            }
          }
        }
      }
    });
  });
}
getFolderTree(file_path).then(function() {
  console.log(final_array); // this is not working 
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我认为我找到了解决方案,但我仍然对它是如何工作感到困惑。

我从另一个代码中获取了参考,并能够找出一些方法。

如果有人有更好的解决方案,请告诉我 还请帮我理解代码

var fs = require('fs');
var path = require('path');
let root_path = "C:\\Users\\HP\\Desktop\\test_folder";

function getAllDirectoriesPath(current_path) {
    var results = [];
    return new Promise(function (resolve, reject) {
        fs.readdir(current_path, function (erro, sub_dirs) {
            if (erro) {
                console.log(error);
            } else {
                let no_of_subdir = sub_dirs.length;
                if (!no_of_subdir) {
                    return resolve(results);
                } else {
                    sub_dirs.forEach(function (dir) {
                        dir = path.resolve(current_path, dir);
                        fs.stat(dir, function (err, stat) {
                            if (stat && stat.isDirectory()) {
                                getAllDirectoriesPath(dir).then(function (res) {
                                    results = results.concat(res);
                                    if (!--no_of_subdir) {
                                        resolve(results);
                                    }
                                });
                            } else {
                                fs.readFile(dir, function (err, data) {
                                    results.push(data.toString());
                                    if (!--no_of_subdir) {
                                        resolve(results);
                                    }
                                });

                            }
                        });
                    });
                }
            }
        });
    });
}

getAllDirectoriesPath(root_path).then(function (results) {
    console.log(results);
});