Home > Web Front-end > JS Tutorial > How Can Promise.all Improve Concurrent Async Operation Handling and Avoid Unhandled Rejections?

How Can Promise.all Improve Concurrent Async Operation Handling and Avoid Unhandled Rejections?

Mary-Kate Olsen
Release: 2024-12-23 17:03:32
Original
720 people have browsed it

How Can Promise.all Improve Concurrent Async Operation Handling and Avoid Unhandled Rejections?

Waiting for Concurrent Async Operations

Parallel Execution with Promise.all

To trigger multiple async operations concurrently, avoid using the pattern where you acquire promises and then await them sequentially. Instead, leverage the Promise.all function:

const [value1, value2] = await Promise.all([getValue1Async(), getValue2Async()]);
Copy after login

This approach initiates both async operations simultaneously and provides the results in an array.

Handling Rejected Promises

The provided solution runs the operations in parallel, but waits for the first to complete before waiting for the second. While this allows parallel execution, it introduces issues in handling rejected promises.

If the first promise takes longer to complete and the second fails, Promise.all will fail immediately, preventing the first promise from rejecting. This can result in an unhandled rejection error.

Syntax for Parallel Await

Currently, there isn't a designated await syntax for parallel waiting, hence the alternative of using Promise.all. Discussions have emerged regarding await.all, indicating the possibility of future enhancements.

Comparison of Approaches

Consider the following example, where getValue1Async takes 500ms to resolve and getValue2Async takes 100ms to reject:

Sequential Execution:

async () => {
  try {
    const value1 = await getValue1Async();
    const value2 = await getValue2Async();
  } catch (e) {
    console.error(e);
  }
};
Copy after login

Concurrent Execution with Promise.all:

async () => {
  try {
    const [value1, value2] = await Promise.all([getValue1Async(), getValue2Async()]);
  } catch (e) {
    console.timeEnd("Promise.all", e);
  }
};
Copy after login

Output:

  • Sequential: Console output shows a delay of 500ms before the error is logged.
  • Concurrent: Console output shows an error log after 100ms.

Conclusion:

Using Promise.all effectively enables parallel execution of async operations and ensures proper handling of rejected promises.

The above is the detailed content of How Can Promise.all Improve Concurrent Async Operation Handling and Avoid Unhandled Rejections?. 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