Home > Web Front-end > JS Tutorial > body text

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

Linda Hamilton
Release: 2024-11-24 03:39:11
Original
157 people have browsed it

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()]);
Copy after login

Storing Results:

To capture the results, employ:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template