等待递归调用异步函数的函数

Awaiting a function that calls an async function recursively

我有一个看起来像这样的函数:

function populateMap(directory: string, map, StringMap) {
    fs.promises.readdir(directory).then(files: string[]) => {
        files.forEach(file: string) => {
            const fullPath = path.join(directory, file);
            fs.stat(fullPath, (err: any, stats: any) => {
                if (stats.isDirectory()) {
                   populateFileMap(fullPath, fileMap);
                } else {
                   fileMap[file] = fullPath;
                }
            });
        });
    });
}

我想做的是递归遍历父目录并将文件名映射存储到它们的路径。我知道这是有效的,因为如果我在 fileMap[file] = fullPath 下放置一个 console.log(fileMap),在目录中最深的文件之后,列表就会被正确填充。

在调用此函数的文件中,我希望能够拥有完整的地图

function populateMapWrapper(dir: string) {
    const fileMap: StringMap = {};

    populateMap(dir, fileMap);

    //fileMap should be correctly populated here
}

我试过使 populateMap 异步,在包装函数中调用它的地方添加 .then(),但是如果我在 then() 函数中 console.log(fileMap),fileMap 是空的.

我不确定这是因为 javascript 传递变量的方式,还是我对 promises 的理解存在差距,但我想知道是否有其他方法可以做到这一点。

一个问题是 fs.stat 没有 return 承诺。您还需要使用 fs.promises.stat。此外,在使用 promise 时要小心使用 forEach,因为它不会 await 用于每个 forEach 回调。您可以改为使用 mapPromise.all()

一个解决方案:

function populateMap(directory: string, map) {
  return fs.promises.readdir(directory).then((files: string[]) => {
    return Promise.all(
      files.map((file: string) => {
        const fullPath = path.join(directory, file);
        return fs.promises.stat(fullPath).then(stats => {
          if (stats.isDirectory()) {
            return populateMap(fullPath, map);
          } else {
            map[file] = fullPath;
          }
        })
      }))
  })
}

那么你必须在包装器中使用 await:

async function populateMapWrapper(dir: string) {
    const fileMap: StringMap = {};

    await populateMap(dir, fileMap);

    //fileMap should be correctly populated here
}

但是,一个更具可读性的解决方案是尽可能使用 await。类似于:

async function populateMap (directory: string, map) {
  const files = await fs.promises.readdir(directory)
  for (const file of files) {
    const fullPath = path.join(directory, file)
    const stats = await fs.promises.stat(fullPath)
    if (stats.isDirectory()) {
      await populateMap(fullPath, map)
    } else {
      map[file] = fullPath
    }
  }
}