CoreUI ONE — 250+ PRO components and 25+ admin templates for React, Angular, Vue, Bootstrap and Next.js, plus Data Grid, Scheduler, 4,300+ PRO icons and the Next.js boilerplate. One license. → See what you get

How to use async/await in React data fetching

Using async/await syntax makes data fetching code more readable and easier to debug compared to promise chains, especially when handling multiple sequential API calls. As the creator of CoreUI, a widely used open-source UI library, I’ve implemented async/await patterns in React components across numerous projects. From my expertise, the most effective approach is creating async functions within useEffect using immediately invoked function expressions (IIFE). This provides clean, synchronous-looking code while maintaining proper React lifecycle management.

Use async/await within useEffect by creating an immediately invoked async function.

const [data, setData] = useState(null)

useEffect(() => {
  (async () => {
    try {
      const response = await fetch('/api/users')
      const result = await response.json()
      setData(result)
    } catch (error) {
      console.error('Error fetching data:', error)
    }
  })()
}, [])

Here the async function is immediately invoked within useEffect since useEffect cannot be async itself. The try/catch block handles errors synchronously, making error handling more intuitive. The await keywords make the code read like synchronous operations while maintaining the asynchronous nature of HTTP requests.

Best Practice Note:

This is the same async pattern we use in CoreUI React components for complex data operations. Always wrap async operations in try/catch blocks and consider extracting complex async logic into custom hooks for better reusability and testing.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Add a Tab in HTML
How to Add a Tab in HTML

How to Open Link in a New Tab in HTML?
How to Open Link in a New Tab in HTML?

How to Clone an Object in JavaScript
How to Clone an Object in JavaScript

How to Fix “Sass @import Rules Are Deprecated and Will Be Removed in Dart Sass 3.0.0.”
How to Fix “Sass @import Rules Are Deprecated and Will Be Removed in Dart Sass 3.0.0.”