> 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/usepromise.md).

# usePromise

The purpose of this hook is to facilitate the consumption of promises.

### Syntax

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

const callback = (id: number) => Promise.resolve(id);
const args: Parameters<typeof callback> = [1];
const autorun = true;

const promise = usePromise( callback, args, autorun );
```

where:

* `callback` : asynchronous function.
* `args`: arguments that callback requires.
* `autorun`: by default true, it automatically executes the promise, if it is false the execution of the promise is suspended.
* `promise` : return object, at the type level it defines the following properties:
  * `pending`: boolean, defines if the promise is executed but pending resolution.
  * `fulfilled`: boolean, defines if the promise has been fulfilled
  * `result`: result of the promise.
  * `rejected`: boolean, defines if the promise has been rejected.

{% hint style="info" %}
**Note**: When using Typescript Do not force the types for the second argument of usePromise, as usePromise will infer them from your callback.

It is preferable to use ternary or if to condition the reading of states, since this will help the definition of result
{% endhint %}

### Example

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

const getUsers = (id: number) => Promise.resolve({ id, name: "Atomico" });

function component() {
  const promise = usePromise(getUsers, [1]);

  return (
    <host>
      {promise.fulfilled ? (
        <h1>Done {promise.result.name}!</h1>
      ) : promise.pending ? (
        <h1>Loading...</h1>
      ) : (
        <h1>Error!</h1>
      )}
    </host>
  );
}
```

{% hint style="info" %}
**Note**: At the type level, autocompletion is conditional on the state you are looking to observe.
{% endhint %}


---

# 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/usepromise.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.
