Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

javascript - 并行调用异步/等待功能(Call async/await functions in parallel)

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.(据我了解,在ES7 / ES2016中,在代码中放置多个await的工作方式类似于将带有.then()的承诺链接在一起,这意味着它们将一个接一个地执行而不是并行执行。)

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?(我是否正确理解只有在someCall()完成后才会调用anotherCall() ?) 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?(我想在Node中使用它,所以也许有一个异步库解决方案?) 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.(编辑:我不满意此问题提供的解决方案: 减速是由于异步生成器中非并行等待promise ,因为它使用生成器,并且我询问的是更通用的用例。)   ask by Victor Marchuk translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can await on Promise.all() :(您可以在Promise.all()上等待:)

await Promise.all([someCall(), anotherCall()]); To store the results:(要存储结果:) let [someResult, anotherResult] = await Promise.all([someCall(), anotherCall()]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...