TypeScript icon, indicating that this package has built-in type declarations

0.0.9 • Public • Published

Build Size Version

npm install suspend-react

This library integrates your async ops into React suspense. Pending- and error-states are handled at the parental level which frees the individual component from that burden and allows for better orchestration. Think of it as async/await for components. Works in all React versions >= 16.6.

import { Suspense } from 'react'
import { suspend } from 'suspend-react'

function Post({ id, version }) {
  const data = suspend(async () => {
    const res = await fetch(`${version}/item/${id}.json`)
    return res.json()    
  }, [id, version])
  return (
      {data.title} by {}

function App() {
  return (
    <Suspense fallback={<div>loading...</div>}>
      <Post id={1000} version="v0" />


const result = suspend((...keys) => Promise<any>, keys: any[], config)

suspend yields control back to React and the render-phase is aborted. It will resume once your promise resolves. For this to work you need to wrap it into a <React.Suspense> block, which requires you to set a fallback (can be null).

The dependencies (the 2nd argument) act as cache-keys, use as many as you want. If an entry is already in cache, calling suspend with the same keys will return it immediately without breaking the render-phase. Cache access is similar to useMemo but across the component tree.

The 1st argument has to be a function that returns a thenable (async function or a promise), it receives the keys as arguments. suspend will return the resolved value, not a promise! This is guaranteed, you do not have to check for validity. Errors will bubble up to the nearest error-boundary.


Both suspend and preload can optionally receive a config object,


The lifespan prop allows you to invalidate items over time, it defaults to 0 (keep-alive forever).

// Keep cached item alive for one minute
suspend(fn, keys, { lifespan: 60000 })
Equality function

The equal prop customizes per-key validation, it defaults to (a, b) => a === b (reference equality).

import equal from 'fast-deep-equal'

// Validate keys deeply
suspend(fn, keys, { equal })


import { preload } from 'suspend-react'

async function fetchFromHN(id, version) {
  const res = await fetch(`${version}/item/${id}.json`)
  return res.json()

preload(fetchFromHN, [1000, 'v0'])

Cache busting

import { clear } from 'suspend-react'

// Clear all cached entries
// Clear a specific entry
clear([1000, 'v0'])

Peeking into entries outside of suspense

import { peek } from 'suspend-react'

// This will either return the value (without suspense!) or undefined
peek([1000, 'v0'])

Making cache-keys unique

Since suspend operates on a global cache (for now, see React 18), you might be wondering if keys could bleed, and yes they would. To establish cache-safety, create unique or semi-unique appendixes.

- suspend(fn, [1000, 'v0'])
+ suspend(fn, [1000, 'v0', 'functionName/fetch'])

If you publish a library that suspends, consider symbols.

const fetchUUID = Symbol()

export function Foo() {
  suspend(fn, [1000, 'v0', fetchUUID])


Correct types will be inferred automatically.

React 18

Suspense, as is, has been a stable part of React since 16.6, but React will likely add some interesting caching and cache busting APIs that could allow you to define cache boundaries declaratively. Expect these to be work for suspend-react once they come out.


Fetching posts from hacker-news: codesandbox

Infinite list: codesandbox


npm i suspend-react

DownloadsWeekly Downloads






Unpacked Size

30.4 kB

Total Files


Last publish


  • drcmda