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

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

问题描述

据我所知,在ES7 / ES2016中,多个等待的代码将类似于链接 .then()具有承诺,这意味着它们将一个接一个地执行,而不是在parallerl中执行。所以,例如,我们有这个代码:

  await someCall(); 
等待anotherCall();

我正确理解 anotherCall()仅当 someCall()完成时才会调用?什么是最优雅的方式并行调用?



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



编辑:我对此问题提供的解决方案不满意:

您可以等待Promise.all():

 等待Promise.all([someCall(),anotherCall()]); 


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 parallerl. So, for example, we have this code:

await someCall();
await anotherCall();

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?

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

EDIT: 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.

解决方案

You can await on Promise.all():

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

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

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