Hone logo
Hone
Problems

Parallel Data Loading and Rendering in Angular

Angular applications often need to fetch and display data from multiple sources. Sequential loading can lead to a sluggish user experience, especially when dealing with numerous or slow-loading datasets. This challenge asks you to implement a component that fetches data from multiple APIs concurrently and renders the results efficiently, improving perceived performance.

Problem Description

You are tasked with creating an Angular component called ParallelDataComponent that fetches data from a configurable array of API endpoints. The component should:

  1. Fetch Data Concurrently: Use Promise.all() or a similar mechanism to initiate requests to all API endpoints simultaneously.
  2. Handle Errors Gracefully: Implement error handling for each API request. If any request fails, it should log the error and continue processing the successful requests. The component should not crash due to a single failed request.
  3. Render Results: Once all requests have completed (either successfully or with errors), the component should display the fetched data in a structured format. The data should be presented as an array of objects, where each object represents the result from a single API endpoint. Each object should contain the endpoint (the URL used for the request) and the data (the response from the API). If a request fails, the data property should be null and an error property should be present with the error message.
  4. Loading Indicator: Display a loading indicator while the data is being fetched. This indicator should disappear once all requests are complete.
  5. Configurable Endpoints: The API endpoints should be passed to the component as an array of strings via the @Input() decorator.

Expected Behavior:

  • The component should display a loading indicator initially.
  • Data should be fetched from all endpoints concurrently.
  • Errors from individual API calls should be logged, but should not prevent the component from rendering the data from successful calls.
  • The component should render an array of objects, each representing the result of a single API call, including error information if applicable.
  • The loading indicator should disappear once all requests are complete.

Edge Cases to Consider:

  • Empty array of endpoints: The component should not attempt any requests and should immediately display an empty result set (or a message indicating no endpoints were provided).
  • API endpoints returning non-JSON responses: The component should handle these gracefully, logging an error and setting the data property to null.
  • Network errors: The component should handle network errors appropriately, logging the error and continuing.
  • Very slow API responses: The component should continue to function correctly even if some API responses are significantly slower than others.

Examples

Example 1:

Input: endpoints = ['https://api.example.com/data1', 'https://api.example.com/data2']
Output:
[
  { endpoint: 'https://api.example.com/data1', data: { name: 'Data 1' }, error: null },
  { endpoint: 'https://api.example.com/data2', data: { value: 123 }, error: null }
]
Explanation: Both API calls succeed and return JSON data. The output is an array of objects, each containing the endpoint URL and the parsed JSON data.

Example 2:

Input: endpoints = ['https://api.example.com/data1', 'https://api.example.com/data2', 'https://api.example.com/data3']
Output:
[
  { endpoint: 'https://api.example.com/data1', data: { name: 'Data 1' }, error: null },
  { endpoint: 'https://api.example.com/data2', data: { value: 123 }, error: null },
  { endpoint: 'https://api.example.com/data3', data: null, error: 'Failed to fetch data from https://api.example.com/data3: Network error' }
]
Explanation: The first two API calls succeed, while the third fails due to a network error. The output includes the successful data and an error object for the failed request.

Example 3:

Input: endpoints = []
Output: []
Explanation: An empty array of endpoints is provided. The component should not make any requests and return an empty array.

Constraints

  • The component must be written in TypeScript and compatible with Angular 14 or later.
  • The component should use Angular's built-in features for data binding and event handling.
  • The API endpoints must be strings.
  • The component should handle errors gracefully and prevent crashes.
  • The component should display a loading indicator while data is being fetched.
  • The component should be reasonably performant, avoiding unnecessary computations or DOM manipulations. Assume a maximum of 10 API endpoints.

Notes

  • Consider using Angular's HttpClient module for making API requests.
  • Think about how to structure your component's template to display the data and the loading indicator effectively.
  • Use appropriate error handling techniques to catch and log errors.
  • The API endpoints are assumed to return JSON data. Handle cases where they don't.
  • Focus on clarity, readability, and maintainability of your code. Good commenting is appreciated.
  • You don't need to implement the actual API endpoints; you can use mock data or simulate API calls with setTimeout for testing purposes.
Loading editor...
typescript