Front-end/React.js(Next.js)

Promise.all() vs. 단일 function을 통한 axios 호출

prden 2023. 6. 22. 21:33

Using Promise.all() is one efficient approach to make multiple API calls concurrently. However, whether it is the most efficient way depends on your specific requirements and circumstances. There are scenarios where calling API requests separately using individual functions might be more suitable.

Here are some factors to consider when deciding the most efficient approach for making multiple API calls:

  1. Concurrency: If you have independent API calls that can be made simultaneously, using Promise.all() allows them to run concurrently, potentially reducing the overall waiting time for the responses.
  2. Dependencies and Order: If your API calls have dependencies or need to be executed in a specific order, using Promise.all() may not be appropriate. In such cases, separate functions for each API call can provide more control over the sequence and dependencies.
  3. Error Handling: With Promise.all(), if any of the API requests fail, the entire promise will be rejected. This behavior may or may not be desired depending on your use case. When calling APIs separately, you have more flexibility in handling errors on an individual basis.
  4. Request Customization: In some cases, you may need to customize headers, parameters, or other request configurations for specific API calls. Making separate functions allows you to easily customize each request independently.
  5. Flexibility and Code Readability: Depending on the complexity of your API calls and data processing, making separate functions might provide more flexibility and maintainability in your codebase. It can improve code readability and make it easier to debug and maintain in the long run.

In summary, using Promise.all() is a powerful approach for concurrent API calls, especially when the requests are independent and order doesn't matter. However, for cases with dependencies, customizations, or specific error handling requirements, calling API requests separately using individual functions can offer more flexibility and control. Consider your specific use case, requirements, and trade-offs to determine the most efficient approach for your application.