This async action to fetch user data has a potential bug. What is the issue?
React JS interview question for Advanced practice.
Answer
The code doesn't handle API errors, which could leave loading as true indefinitely.
Explanation
The action correctly sets loading to true, but it lacks a try...catch block. If the fetch call fails (e.g., network error, 404), the code after await will not execute, and the loading state will remain true forever. All async operations should be wrapped in try...catch to handle errors gracefully.