Asynchronous Data Fetcher with Async/Await
This challenge focuses on implementing an asynchronous data fetching service in Rust using async/await. Asynchronous programming is crucial for building responsive and efficient applications, especially when dealing with I/O-bound operations like network requests. Your task is to create a simple data fetcher that retrieves data from a simulated API and handles potential errors gracefully.
Problem Description
You need to implement a module named data_fetcher that provides an asynchronous function fetch_data which simulates fetching data from an external API. The fetch_data function should take a task_id (an integer) as input and return a Result<String, String>. The function should simulate a network request with a random delay (between 100ms and 500ms) using tokio::time::sleep. Based on the task_id, the function should either return a success message (if task_id is even) or an error message (if task_id is odd).
Key Requirements:
- Asynchronous Operation: The
fetch_datafunction must be asynchronous and useasync/await. - Error Handling: The function must return a
Resulttype to indicate success or failure. UseStringfor both the success and error messages. - Simulated Delay: Introduce a random delay between 100ms and 500ms to simulate network latency.
- Conditional Logic: Return a success message if the
task_idis even, and an error message if it's odd. - Tokio Dependency: Utilize the
tokioruntime for asynchronous execution.
Expected Behavior:
- When
task_idis even,fetch_datashould returnOk("Data fetched successfully for task_id: [task_id]")after a random delay. - When
task_idis odd,fetch_datashould returnErr("Failed to fetch data for task_id: [task_id]")after a random delay. - The function should not block the main thread during execution.
Edge Cases to Consider:
- Handle potential errors during the simulated delay (though this is unlikely in this simplified scenario).
- Ensure the code is well-structured and easy to understand.
Examples
Example 1:
Input: task_id = 2
Output: Ok("Data fetched successfully for task_id: 2")
Explanation: The task_id is even, so the function returns a success message after a random delay.
Example 2:
Input: task_id = 3
Output: Err("Failed to fetch data for task_id: 3")
Explanation: The task_id is odd, so the function returns an error message after a random delay.
Example 3: (Edge Case)
Input: task_id = 0
Output: Ok("Data fetched successfully for task_id: 0")
Explanation: The task_id is even (0 is considered even), so the function returns a success message after a random delay.
Constraints
- The simulated delay must be between 100ms and 500ms (inclusive).
- The
task_idwill always be an integer. - The code must compile and run without panicking.
- The solution should be concise and readable.
Notes
- You'll need to add
tokioas a dependency in yourCargo.tomlfile. - Consider using
rand::random()to generate the random delay. - Remember to use
#[tokio::main]to mark your main function as an asynchronous entry point. - Focus on demonstrating the correct usage of
async/awaitand error handling within an asynchronous context. The simulated API and random delay are primarily for demonstrating asynchronous behavior.