Course contentsShow
← React
Lesson 1345 of 2,39634. Suspense FundamentalsFree lesson

Suspense Without a Data Library

You can build Promise-throwing components manually, but libraries like TanStack Query or frameworks handle it better.

Suspense Without a Data Library

What You'll Learn: You'll discover that while it's technically possible to manually create Suspense-compatible components by throwing promises, real-world applications benefit from using proper data libraries that handle this complexity for you.

The Manual Approach: Throwing Promises

Suspense works by catching thrown promises during render. You can implement this yourself:

let dataCache = null;
let dataPromise = null;

function ManualSuspenseComponent() {
  if (dataCache) {
    return <div>{dataCache}</div>;
  }
  
  if (!dataPromise) {
    dataPromise = fetchData().then(result => {
      dataCache = result;
    });
  }
  
  throw dataPromise; // Suspense catches this
}

// Usage
<Suspense fallback={<Spinner />}>
  <ManualSuspenseComponent />
</Suspense>

This works! When React encounters the thrown promise, the nearest Suspense boundary displays the fallback. When the promise resolves, React retries rendering.

Why This Gets Messy Fast

Managing module-level caches like this creates problems:

  • No cache invalidation strategy – stale data lives forever
  • No error handling – what if the promise rejects?
  • No refetching logic – can't trigger updates
  • Memory management – cache grows indefinitely
  • Race conditions – concurrent requests interfere
  • No request deduplication – multiple components trigger duplicate fetches

The Better Path

Libraries like TanStack Query (React Query) or frameworks like Next.js and Remix provide battle-tested Suspense integration. They handle caching, refetching, error boundaries, race conditions, and memory management automatically.

Key Takeaway: Throwing promises manually demonstrates how Suspense works under the hood, but production code should use established data libraries or framework primitives that solve the complex problems of cache management, error handling, and state coordination.