Medium javascript

useEffect fetch pattern (pseudo)

Problem

Describe fetch-on-mount: set loading, then data—implement with async IIFE.

Hints
  • async IIFE inside effect; set state after await

Your practice code

Ready — edit the code above and click Run.

Solution

async function loadData() {
  return { id: 1, name: "Demo" };
}
(async () => {
  let loading = true;
  let data = null;
  data = await loadData();
  loading = false;
  console.log({ loading, data });
})();

Try solving on your own first, then reveal the official answer.

Explanation

Mount effect with cleanup is a top React interview topic—always mention abort/cancel for fetch.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details