# Use the cache outside Effect (https://0758e0c7.effetch-query.pages.dev/docs/cache/outside-effect)

Code outside Effect, such as a router loader or an event handler, can't
`yield*` a cache function ([Use the cache from Effect](https://0758e0c7.effetch-query.pages.dev/docs/cache)). The
client has a method for each, which returns
a `Promise` and does what its `Query` function does:

The examples import `client.ts` and `todos.ts` from [Getting started](https://0758e0c7.effetch-query.pages.dev/docs/getting-started).

| Method                      | Returns                                                                         |
| --------------------------- | ------------------------------------------------------------------------------- |
| `client.fetch(query)`       | `Promise<A>`, rejected as the query fails, so a router's redirect gets through. |
| `client.prefetch(query)`    | `Promise<void>`, resolved when the fetch settles. Never rejects.                |
| `client.invalidate(target)` | `Promise<void>`, resolved at once.                                              |
| `client.refetch(target)`    | `Promise<void>`, resolved when the queries in use have settled.                 |
| `client.reset(target)`      | `Promise<void>`, resolved at once.                                              |
| `client.clear(target)`      | `Promise<void>`, resolved at once.                                              |

Here they are in a loader and two event handlers:

```ts
import { queryClient } from "./client.js";
import { todo, todos } from "./todos.js";

// A router loader. It rejects as the query fails, and stops waiting when
// the navigation is cancelled.
export const loader = (id: number, signal: AbortSignal) =>
  queryClient.fetch(todo({ params: { id } }), { signal });

// Hovering a link starts the next screen's fetch. It never rejects.
export const onHoverOpenTodos = () => {
  void queryClient.prefetch(todos({ query: { done: false } }));
};

// Pull to refresh: the spinner stops once the lists in use have settled.
export const onRefresh = () => queryClient.refetch(todos);
```

Without a target, `invalidate`, `refetch`, `reset` and `clear` act on every
query. They reject only with a defect, such as a
`Query.KeyHoldsTargetError` for
a key that holds queries. On a disposed client, they do nothing. `fetch`
and `prefetch` take `{ signal }` to stop waiting when it aborts.

**Read without waiting.** To read the cache where you can't wait for a
promise, such as a form listener, `QueryClient.get(queryClient, query)`
returns the cached value as an `Option`, as `Query.get` does. It never
fetches:

```ts
import * as Option from "effect/Option";

import { QueryClient } from "@monarcode/effetch-query";

import { queryClient } from "./client.js";
import { todo } from "./todos.js";

export const cachedTitle = (id: number): string =>
  Option.match(QueryClient.get(queryClient, todo({ params: { id } })), {
    onNone: () => "",
    onSome: (cached) => cached.title,
  });
```

**Run any Effect.** For anything else, `queryClient.runPromise(effect)` runs
an Effect with your services and the client, and rejects with its error if
it fails. `queryClient.runPromiseExit(effect)` resolves with the `Exit`
instead, so it never rejects. Both take `{ signal }` to interrupt the
Effect when the signal aborts. In a component, `useQueryClient()` returns
the client.

When you're done with a client, such as one made per request on a server,
`queryClient.dispose()` releases everything it owns
([Client members](https://0758e0c7.effetch-query.pages.dev/docs/client/members)).