利用async/await与forEach循环进行操作
P粉010967136
P粉010967136 2023-08-20 11:27:20
0
2
377
<p>在使用 <code>forEach</code> 循环中使用 <code>async</code>/<code>await</code> 会有什么问题吗?我试图遍历一个文件数组,并在每个文件的内容上使用 <code>await</code>。</p> <pre class="brush:php;toolbar:false;">import fs from 'fs-promise' async function printFiles () { const files = await getFilePaths() // 假设这个函数正常工作 files.forEach(async (file) =&gt; { const contents = await fs.readFile(file, 'utf8') console.log(contents) }) } printFiles()</pre> <p>这段代码确实可以工作,但是这样做会有什么问题吗?有人告诉我在像这样的高阶函数中不应该使用 <code>async</code>/<code>await</code>,所以我想问一下是否有任何问题。</p>
P粉010967136
P粉010967136

Antworte allen(2)
P粉697408921

使用ES2018,您可以大大简化上述所有答案:

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

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

查看规范:proposal-async-iteration

简化后:

for await (const results of array) {
    await longRunningTask()
  }
  console.log('I will wait')

2018-09-10:最近这个答案引起了很多关注,请参阅Axel Rauschmayer的博客文章以获取有关异步迭代的更多信息。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!