> For the complete documentation index, see [llms.txt](https://atomico.gitbook.io/doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://atomico.gitbook.io/doc/api/hooks/useasync-and-usesuspense.md).

# useAsync and useSuspense

## useAsync

with a similar approach to React's use hook, **but with scope approach**.

**scope approach?** yes, this hook seeks to resolve a promise from a callback return, this allows you to regenerate the promise according to the scope, example:

{% tabs %}
{% tab title="JSX" %}

```tsx
import { useAsync } from "atomico";

const getUser = (userId) => fetch(`users/${userId}`).then((res) => res.json());

function component({ userId }) {
  const user = useAsync(getUser, [userId]);
  return (
    <host>
      <h1>name: {user.name}</h1>
    </host>
  );
}

component.props = { userId: Number }
```

{% endtab %}

{% tab title="TSX" %}

```tsx
import { Props, useAsync } from "atomico";

const getUser = (userId: number): Promise<{ name: string }> =>
  fetch(`users/${userId}`).then((res) => res.json());

function component({ userId }: Props<typeof component>) {
  const user = useAsync(getUser, [userId]);
  return (
    <host>
      <h1>name: {user.name}</h1>
    </host>
  );
}

component.props = { userId: Number };
```

{% endtab %}
{% endtabs %}

where:

* `getUser`: async callback.
* `[ userId ]`: arguments that if changed regenerate the promise.
* `user` : promise return

Like useEffect, the promise will be executed every time the arguments to the second parameter of useAsync change.

{% hint style="warning" %}
Rendering will be suspended until the promise is resolved or rejected, the resolution of the promises can be observed with useSuspense
{% endhint %}

## useSuspense

allows to listen to all useAsync executions nested in the component, example:

```tsx
function component() {
  const status = useSuspense();
  return (
    <host shadowDom>
      {status.pending ? "Loading..." : status.fulfilled ? "Done!" : "Error!"}~
      <slot />
    </host>
  );
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://atomico.gitbook.io/doc/api/hooks/useasync-and-usesuspense.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
