Hone logo
Hone
Problems

Asynchronous Data Fetching with Angular's forkJoin

Angular's forkJoin operator provides a concise and efficient way to subscribe to multiple asynchronous observables and wait for all of them to complete. This challenge asks you to implement a component that utilizes forkJoin to fetch data from multiple APIs concurrently and display the combined results. This is a common pattern in Angular applications for loading related data in a single request, improving performance and user experience.

Problem Description

You need to create an Angular component that fetches data from three different APIs: api/users, api/posts, and api/comments. Each API returns a different type of data (users, posts, and comments respectively). The component should use forkJoin to subscribe to all three observables concurrently. Once all three observables have completed and emitted their values, the component should combine the results into a single object and display it in the template.

Key Requirements:

  • Fetch Data: Implement three separate HTTP requests to the specified API endpoints. Assume these endpoints return JSON data.
  • Use forkJoin: Utilize Angular's forkJoin operator to subscribe to all three observables simultaneously.
  • Combine Results: After forkJoin completes, combine the results from the three observables into a single object with the following structure: { users: usersData, posts: postsData, comments: commentsData }.
  • Display Data: Display the combined data in the component's template. For simplicity, display the length of each array (users, posts, comments).
  • Error Handling: Implement basic error handling. If any of the HTTP requests fail, display an error message in the template.

Expected Behavior:

  1. The component should load when the application starts.
  2. Three HTTP requests should be initiated concurrently.
  3. The component should wait for all three requests to complete.
  4. Once all requests are complete, the combined data should be displayed.
  5. If any request fails, an error message should be displayed.

Edge Cases to Consider:

  • Network Errors: Handle cases where the network is unavailable or the API endpoints are unreachable.
  • Empty Responses: Consider what should happen if one or more of the APIs return empty arrays.
  • API Errors: Handle cases where the APIs return error responses (e.g., 404 Not Found, 500 Internal Server Error).

Examples

Example 1:

Input:  api/users returns [{id: 1, name: "Alice"}, {id: 2, name: "Bob"}], api/posts returns [{id: 1, title: "Post 1"}, {id: 2, title: "Post 2"}], api/comments returns [{id: 1, text: "Comment 1"}, {id: 2, text: "Comment 2"}]
Output:  { users: 2, posts: 2, comments: 2 } displayed in the template.
Explanation: forkJoin waits for all three requests to complete. The combined object is created and the lengths of the arrays are displayed.

Example 2:

Input: api/users returns [], api/posts returns [{id: 1, title: "Post 1"}], api/comments returns [{id: 1, text: "Comment 1"}]
Output: { users: 0, posts: 1, comments: 1 } displayed in the template.
Explanation: forkJoin waits for all three requests to complete. The combined object is created, and the lengths of the arrays are displayed, even if some arrays are empty.

Example 3: (Error Handling)

Input: api/users returns an error (e.g., 500 Internal Server Error)
Output: An error message "Error fetching data" is displayed in the template.
Explanation: The forkJoin operator will catch the error from the user API request and propagate it. The error handling logic in the component will display the error message.

Constraints

  • Angular Version: Angular 14 or higher.
  • HTTP Client: Use Angular's HttpClient module for making HTTP requests.
  • RxJS: Utilize RxJS operators, specifically forkJoin.
  • Data Format: Assume the APIs return JSON data.
  • API Endpoints: Use the following placeholder API endpoints: api/users, api/posts, api/comments. You do not need to implement these APIs; you can use mock data or a service that returns static data for testing purposes. The focus is on using forkJoin correctly.
  • Performance: The solution should be reasonably efficient. Avoid unnecessary computations or DOM manipulations.

Notes

  • Consider using a service to encapsulate the HTTP requests for better organization and testability.
  • The template can be simple; displaying the lengths of the arrays is sufficient.
  • Focus on correctly implementing forkJoin and handling potential errors.
  • Remember to import the necessary RxJS operators (e.g., forkJoin) and Angular modules (e.g., HttpClientModule).
  • Think about how to handle the asynchronous nature of the HTTP requests and the forkJoin operator.
Loading editor...
typescript