将 async/await 与 forEach 循环一起使用

forEach 循环中使用 async/await 有什么问题吗?我正在尝试遍历文件数组并 await 每个文件的内容.

Are there any issues with using async/await in a forEach loop? I'm trying to loop through an array of files and await on the contents of each file.

import fs from 'fs-promise'

async function printFiles () {
  const files = await getFilePaths() // Assume this works fine

  files.forEach(async (file) => {
    const contents = await fs.readFile(file, 'utf8')
    console.log(contents)
  })
}

printFiles()

此代码确实有效,但会出现问题吗?有人告诉我你不应该在这样的高阶函数中使用 async/await,所以我只是想问一下是否有任何问题有了这个.

This code does work, but could something go wrong with this? I had someone tell me that you're not supposed to use async/await in a higher-order function like this, so I just wanted to ask if there was any issue with this.

推荐答案

当然代码可以正常工作,但我很确定它没有按照您的预期执行.它只是触发多个异步调用,但 printFiles 函数会在此之后立即返回.

Sure the code does work, but I'm pretty sure it doesn't do what you expect it to do. It just fires off multiple asynchronous calls, but the printFiles function does immediately return after that.

如果要按顺序读取文件,确实不能使用forEach.只需使用现代的 for ... of 循环,其中 await 将按预期工作:

If you want to read the files in sequence, you cannot use forEach indeed. Just use a modern for … of loop instead, in which await will work as expected:

async function printFiles () {
  const files = await getFilePaths();

  for (const file of files) {
    const contents = await fs.readFile(file, 'utf8');
    console.log(contents);
  }
}

并行读取

如果您想并行读取文件,确实不能使用 forEach.每个 async 回调函数调用都会返回一个 Promise,但您将它们扔掉而不是等待它们.只需使用 map 代替,您就可以等待通过 Promise.all 获得的一系列承诺:

Reading in parallel

If you want to read the files in parallel, you cannot use forEach indeed. Each of the async callback function calls does return a promise, but you're throwing them away instead of awaiting them. Just use map instead, and you can await the array of promises that you'll get with Promise.all:

async function printFiles () {
  const files = await getFilePaths();

  await Promise.all(files.map(async (file) => {
    const contents = await fs.readFile(file, 'utf8')
    console.log(contents)
  }));
}

相关文章