gpt4 book ai didi

javascript - 有没有一种干净的方法可以无限使用异步函数?

转载 作者:行者123 更新时间:2023-11-30 08:24:10 25 4
gpt4 key购买 nike

根据 ESLint,像这样的一些代码不是“干净的代码”

for(;;) {
await *async function*
}

我的目标是无限循环某个函数,一个一个地执行它,而不会因为调用堆栈限制而最终导致我的应用程序崩溃。我已经考虑了一段时间,但无法想出任何其他可以做同样的事情。 ESLint 的建议对我来说也行不通;他们建议启动循环中的所有函数,并使用 .all() 在循环外等待它们的解析/拒绝回调。

帮助将不胜感激!我只想尽可能干净地写这个

最佳答案

正如 ESLint 文档所说:

In many cases the iterations of a loop are not actually independent of each-other. For example, the output of one iteration might be used as the input to another. Or, loops may be used to retry asynchronous operations that were unsuccessful. In such cases it makes sense to use await within a loop and it is recommended to disable the rule via a standard ESLint disable comment.

因此,如果您认为每次迭代都需要等待,请禁用此规则。如果您可以并行化异步调用,请使用 Promise.all

要仅在代码中的某个位置禁用 ESLint 规则,请按如下方式执行:

/* eslint-disable no-await-in-loop */
//Your code here...
/* eslint-enable no-await-in-loop */

关于javascript - 有没有一种干净的方法可以无限使用异步函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48370317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com