---
title: "Recipe: fetch with loading, error and retry"
description: "A fetched resource with loading, an error and Retry, whose rows belong to the owner that asked for them."
modificationDate: "2026-11-24"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: fetch with loading, error and retry

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/fetch-loading-error: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

```prompt Show loading, error and retry for a fetch
Make a screen of the Exact app in this folder show loading, an error with Try Again, and the empty state for data it fetches. Use https://github.com/expo/exact/blob/main/docs/recipes/fetch-loading-error.md as the source for each step. The Exact checkout has the same page at docs/recipes/fetch-loading-error.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. If I did not say which data, list the resources in app.contract that fetch from the network and ask me which one. If the app fetches nothing yet, ask me for the URL; do not invent one.
3. In app.ts, add the URL to `grants` (`net.fetch <origin>`), set `exactTimeout` on the fetch, throw when the response is not ok, and check the response's shape before you return it.
4. In app.contract, draw a `progress` while `pending`, a message and a Try Again button while `failed`, and an empty-state line when the answer is valid and empty. Give the scroll `refresh` and `refreshing`. A failed refresh keeps the old rows beside Try Again.
5. If the resource takes an account, a server or a workspace, the answer names its owner, and the screen shows rows only when the owner matches the current one. An owner is never empty.
6. Add a test in app.test.contract like the recipe's: `fail fetch` on the origin, refresh, expect the loading state, the kept rows and the error; then `pass fetch`, tap Try Again, and expect no error. Use `answer fetch "<prefix>" with 200 {"text":"Hello"}` for a response fixture, with the status and body the source expects.
7. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 8) and report it; do not keep trying.
8. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
9. Tell me which screen changed and what it shows in each state, what the tests and verify reported, and the launch command for each target platform.
```

```contract
shape Feed
  owner: string
  posts: string[]

component App
  state account = "default"
  resource feed = loadFeed(account) as shape Feed else empty()
  action reload
    refresh feed
  view
    column
      button press=reload testId="refresh"
        text "Refresh"
      scroll refresh=reload refreshing=(pending(feed) and feed.owner == account) flex=1 min-height=0
        if pending(feed)
          progress aria-label="Loading" testId="loading"
        if failed(feed)
          text "Couldn't load the feed." testId="error"
          button press=reload testId="retry"
            text "Try Again"
        if feed.owner == account
          if feed.posts.length == 0 and not failed(feed)
            text "No posts yet."
          for p, i in feed.posts key=p
            text p testId=`post-${i}`
```

`empty().owner` is `""`. Use nonempty IDs. A [failed refresh](https://exact.expo.dev/recipes/progressive-tabs.md) keeps the owner’s rows. Empty replies clear them.

```ts
import type { Answer, Result } from './app.contract.d.ts';

const API = 'https://jsonplaceholder.typicode.com';
export const grants = `net.fetch ${API}`;

function isPost(p: unknown): p is { title: string } {
  return p !== null && typeof p === 'object' && 'title' in p && typeof p.title === 'string';
}

export const answer: Answer = async (_source, [owner]): Promise<Result<'loadFeed'>> => {
  const response = await fetch(`${API}/posts?_limit=10`, { exactTimeout: 10000 });
  if (!response.ok) throw new Error('The feed request failed.');
  const body: unknown = await response.json();
  if (!Array.isArray(body) || !body.every(isPost)) throw new Error('Invalid feed');
  return { owner, posts: body.map((p): string => p.title) };
};
```

```contract-test
test "a failed refresh retains rows and retries"
  clock data
  fail fetch "https://jsonplaceholder.typicode.com"
  tap "refresh"
  expect tree has "post-0"
  clock data
  expect tree has "post-0"
  expect tree has "error"
  pass fetch "https://jsonplaceholder.typicode.com"
  tap "retry"
  clock data
  expect tree missing "error"
```

`fail fetch` rejects before `tap` returns. Test loading by holding a reply on a
[test server](https://exact.expo.dev/recipes/rest-backend.md). To supply a response, use
`answer fetch "<prefix>" with 200 {"text":"Hello"}`.

For private data, check principal and auth generation again after an await before
caching. A → B → A invalidates A's first answer. Await dependencies in the
[source](https://exact.expo.dev/learn/contract-for-agents/data-requests-and-side-effects.md).

Reading `native.available` or calling `native.call` can retain an answer across
launches. Keep [native access](https://exact.expo.dev/recipes/native-module.md) out of fetch sources. Inspect
retention in `state.resourceStatus`. `with` controls boot admission. For a
launch-supplied endpoint, see `contract vocab env`.

On the web, `net.fetch self` admits `fetch('/_data/page.json', {credentials: 'omit'})`
on each tenant. Pass its validated absolute page URL to the renderer, or set
`app.origin` at build time. Native sources need absolute URLs and their own network grants.
