并行调用 async/await 函数 [英] Call async/await functions in parallel

查看:65
本文介绍了并行调用 async/await 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在 ES7/ES2016 中,在代码中放置多个 await 的工作方式类似于将 .then() 与 promise 链接起来,这意味着它们将一个接一个执行,而不是并行执行.例如,我们有这样的代码:

As far as I understand, in ES7/ES2016 putting multiple await's in code will work similar to chaining .then() with promises, meaning that they will execute one after the other rather than in parallel. So, for example, we have this code:

await someCall();
await anotherCall();

我是否正确理解 anotherCall() 只有在 someCall() 完成时才会被调用?并行调用它们的最优雅方式是什么?

Do I understand it correctly that anotherCall() will be called only when someCall() is completed? What is the most elegant way of calling them in parallel?

我想在 Node 中使用它,所以也许有异步库的解决方案?

I want to use it in Node, so maybe there's a solution with async library?

我对这个问题中提供的解决方案不满意:由于异步生成器中的 promise 的非并行等待而导致速度变慢,因为它使用生成器,我正在询问更一般的用例.

I'm not satisfied with the solution provided in this question: Slowdown due to non-parallel awaiting of promises in async generators, because it uses generators and I'm asking about a more general use case.

推荐答案

你可以在 Promise.all() 上等待:

await Promise.all([someCall(), anotherCall()]);

存储结果:

let [someResult, anotherResult] = await Promise.all([someCall(), anotherCall()]);

请注意,Promise.all 很快就会失败,这意味着一旦提供给它的承诺之一被拒绝,整个事情就会被拒绝.

Note that Promise.all fails fast, which means that as soon as one of the promises supplied to it rejects, then the entire thing rejects.

const happy = (v, ms) => new Promise((resolve) => setTimeout(() => resolve(v), ms))
const sad = (v, ms) => new Promise((_, reject) => setTimeout(() => reject(v), ms))

Promise.all([happy('happy', 100), sad('sad', 50)])
  .then(console.log).catch(console.log) // 'sad'

相反,如果您想等待所有承诺履行或拒绝,那么您可以使用 Promise.allSettled.请注意,Internet Explorer 本身并不支持此方法.

If, instead, you want to wait for all the promises to either fulfill or reject, then you can use Promise.allSettled. Note that Internet Explorer does not natively support this method.

const happy = (v, ms) => new Promise((resolve) => setTimeout(() => resolve(v), ms))
const sad = (v, ms) => new Promise((_, reject) => setTimeout(() => reject(v), ms))

Promise.allSettled([happy('happy', 100), sad('sad', 50)])
  .then(console.log) // [{ "status":"fulfilled", "value":"happy" }, { "status":"rejected", "reason":"sad" }]

注意:如果您使用 Promise.all 操作,在拒绝发生之前设法完成的操作不会回滚,因此您可能需要注意这种情况.例如如果你有 5 个动作,4 个快,1 个慢和慢拒绝.那些 4操作可能已经执行,因此您可能需要回滚.在这种情况下,请考虑使用 Promise.allSettled,同时它会提供准确的细节,哪些操作失败,哪些没有.

Note: If you use Promise.all actions that managed to finish before rejection happen are not rolled back, so you may need to take care of such situation. For example if you have 5 actions, 4 quick, 1 slow and slow rejects. Those 4 actions may be already executed so you may need to roll back. In such situation consider using Promise.allSettled while it will provide exact detail which action failed and which not.

这篇关于并行调用 async/await 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆