Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

javascript - 在forEach循环中使用异步/等待(Using async/await with a forEach loop)

Are there any issues with using async / await in a forEach loop?

(在forEach循环中使用async / await是否有任何问题?)

I'm trying to loop through an array of files and await on the contents of each file.

(我正在尝试遍历文件数组并await每个文件的内容。)

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()

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.

(我让某人告诉我,您不应该在这样的高阶函数中使用async / await ,所以我只想问一下这是否有问题。)

  ask by saadq translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(它只是触发多个异步调用,但是在printFiles之后printFiles函数确实会立即返回。)

If you want to read the files in sequence, you cannot use forEach indeed.

(如果forEach顺序读取文件, 实际上不能使用forEach 。)

Just use a modern for … of loop instead, in which await will work as expected:

(只需使用现代的for … of循环,在其中await将按预期工作:)

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

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

If you want to read the files in parallel, you cannot use forEach indeed.

(如果要并行读取文件, 实际上不能使用forEach 。)

Each of the async callback function calls does return a promise, but you're throwing them away instead of awaiting them.

(每个async回调函数调用的确会返回一个Promise,但是您将它们扔掉了,而不是等待它们。)

Just use map instead, and you can await the array of promises that you'll get with Promise.all :

(只需使用map ,您可以等待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)
  }));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...