Fetching Data with Modern JavaScript
Fetching Data with Modern JavaScript
Working with APIs is a key part of building dynamic web apps, and modern JavaScript makes it easier than ever. In this example, we’re using async/await to simplify asynchronous code, try/catch for error handling, and optional chaining to safely access object properties.
Why This Matters
Using async/await instead of traditional promises leads to more readable, cleaner code. Combined with useEffect and useState from React, it becomes straightforward to fetch and display data in a component.
The Optional Chaining Advantage
Notice the use of user?.name and user?.email. This is called optional chaining. It helps prevent runtime errors if the user object is still null when rendering begins. It’s one of the most useful additions to modern JavaScript.
Try It Yourself
Replace the API URL with your own endpoint, and experiment with displaying different user data or loading states. This pattern is common in real-world apps, from dashboards to social feeds.
–EG