Home  >  Article  >  Web Front-end  >  Does `async/await` Sequentially Execute Multiple `await` Calls Like Chained Promises?

Does `async/await` Sequentially Execute Multiple `await` Calls Like Chained Promises?

Linda Hamilton
Linda HamiltonOriginal
2024-11-24 03:39:11155browse

Does `async/await` Sequentially Execute Multiple `await` Calls Like Chained Promises?

Executing Async/Await Functions Concurrently

Question:

When employing async/await in ES7/ES2016, does the sequential execution of multiple awaits mirror that of chaining .then() with promises? Specifically, will anotherCall() only commence upon the completion of someCall()?

Answer:

You have correctly interpreted the sequential nature of await.

Solution for Concurrent Execution:

To execute someCall() and anotherCall() concurrently, utilize Promise.all():

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

Storing Results:

To capture the results, employ:

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

Note:

Keep in mind that Promise.all() fails promptly if any of its supplied promises reject.

The above is the detailed content of Does `async/await` Sequentially Execute Multiple `await` Calls Like Chained Promises?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn