在 forEach 循环中使用 async/await [英] Using async/await with a forEach loop
问题描述
在 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
回调函数调用都会返回一个承诺,但是您将它们扔掉而不是等待它们.只需使用 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)
}));
}
这篇关于在 forEach 循环中使用 async/await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!