并行调用async / await函数 [英] Call async/await functions in parallel
问题描述
等待
的代码将类似于链接 .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屋!