【问题标题】:Async Recursive File System Traversal异步递归文件系统遍历
【发布时间】:2018-07-24 03:13:21
【问题描述】:

我正在尝试从某些特定文件中存在的数据中编译出一个数组,使用递归方法来读取目录,并且文件系统方法是异步的。我无法找出回调调用的合适位置。

const fs = require('fs');
const ENTRY =  "../a/b";
const FILE_NAME = 'index.json';
var nodes = [];

function doThisOnceDone() {
    console.log(nodes);
}

function readFile(path) {
    fs.readFile(path + '/' + FILE_NAME,{
        encoding:"UTF-8"
    }, function(err, data) {
        if(err) {
            return;
        }
        nodes.push(data);
    });
}
function compileArray(path, callback) {
    fs.readdir(path, {
        encoding:"UTF-8"
    }, function(err, files) {
        if(err) {
            console.error(err);
            return;
        }
        files.forEach(function(file) {
            var nextPath = path + '/' + file;
            fs.stat(nextPath, function(err, stats) {
                if(err) {
                    return;
                }
                if(stats.isDirectory()) {
                    if(file === 'specific') {
                        readFile(nextPath);
                    }
                    else {
                        compileArray(nextPath);
                    }
                }
            });
        });
    });
}

compileArray(ENTRY, doThisOnceDone);

我什么时候知道递归树已经完成,我可以访问节点数组了?

【问题讨论】:

  • 对 Util.promisify 使用异步等待。

标签: node.js fs


【解决方案1】:

试试这个

const util = require('util');
const fs = require('fs');
const stat = util.promisify(fs.stat);
const readFile = util.promisify(fs.readFile);
const readdir = util.promisify(fs.readdir);

const ENTRY = "../a/b";
const FILE_NAME = 'index.json';
var nodes = [];

const compileArray = async (path) => {
    try {
        const files = await readdir(path);
        files.forEach((file) => {
            try {
                var nextPath = path + '/' + file;
                const stats = await stat(nextPath); //check other conditions
                if (file === 'specific') {
                    const data = await readFile(path + '/' + FILE_NAME, {
                        encoding: "UTF-8"
                    });
                    nodes.push(data);
                }
            } catch (error) {
                console.log(error);
            }
        });
    } catch (error) {
        console.log(error);
    }
}

【讨论】:

  • 在 const stats = await stat(nextPath) 处中断; SyntaxError: 意外的标识符
  • 谢谢,我想了解你提出的问题。
  • const stats = await stat(nextPath);本质上不在异步函数中,所以我猜是中断。
【解决方案2】:
    const util = require('util');
    const fs = require('fs');
    const stat = util.promisify(fs.stat);
    const readFile = util.promisify(fs.readFile);
    const readdir = util.promisify(fs.readdir);

    const ENTRY = "../a/b";
    const FILE_NAME = 'index.json';
    var nodes = [];

    const compileArray = async (path) => {
        try {
            const files = await readdir(path);
            files.forEach(async (file) => {
                try {
                    var nextPath = path + '/' + file;
                    const stats = await stat(nextPath); //check other conditions
                    if (file === 'specific') {
                        const data = await readFile(path + '/' + FILE_NAME, {
                            encoding: "UTF-8"
                        });
                        nodes.push(data);
                    }
                } catch (error) {
                    console.log(error);
                }
            });
        } catch (error) {
            console.log(error);
        }
    }

compileArray(ENTRY)

【讨论】:

  • await 只能在异步函数中使用。
  • 我还在想,一旦递归完成,我在哪里访问节点数组。
猜你喜欢
  • 1970-01-01
  • 2016-03-12
  • 1970-01-01
  • 2017-04-19
  • 2011-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多