Using async/await with a forEach loop(将 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)
}));
}
这篇关于将 async/await 与 forEach 循环一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 async/await 与 forEach 循环一起使用
基础教程推荐
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 动态更新多个选择框 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06