# Async

Source code is hosted in [GitHub](https://github.com/corets/async)

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

```bash
yarn add @corets/async
```

{% endtab %}

{% tab title="npm" %}

```
npm install --save @corets/async
```

{% endtab %}
{% endtabs %}

Seamless React integrations are shipped inside separate packages:

{% content-ref url="/pages/-MfJ4qThZ0llSH30K7PI" %}
[useAsync](/hooks/use-async.md)
{% endcontent-ref %}

{% content-ref url="/pages/-MeeFDXYVzHIrXuHDnFE" %}
[useStream](/hooks/use-stream.md)
{% endcontent-ref %}

{% content-ref url="/pages/-MeeFDVtWtL6cM6Te5\_Y" %}
[useAction](/hooks/use-action.md)
{% endcontent-ref %}

## createAsync()

Create an observable async operation:

```typescript
import { createAsync } from "@corets/async"

const query = createAsync(async () => "some data")
```

Create an observable async operation without the factory function:

```typescript
import { Async } from "@corets/async"

const query = new Async(async () => "some data")
```

## Async.getResult()

Retrieve result from the last invocation:

```typescript
query.getResult()
```

## Async.getError()

Retrieve probable error from the last invocation:

```typescript
query.getError()
```

## Async.getState()

Retrieve all the details in a single call, contains values like `isRunning`, `isCancelled`, `result`, etc.:

```typescript
query.getState()
```

## Async.isRunning()

Check if the async operation is being executed right now:

```typescript
query.isRunning()
```

## Async.isCancelled()

Check if the latest async invocation has been cancelled:

```typescript
query.isCancelled()
```

## Async.isErrored()

Check if the latest async invocation errored:

```typescript
query.isErrored()
```

## Async.run()

Invoke async operation:

```typescript
import { createAsync } from "@corets/async"

const query = createAsync(async (argument: number) => "some data")
const queryWithArguments = createAsync(async (argument: number) => `The value is ${argument}`)

const result = await query.run()
const anotherResult = await queryWithArguments.run(1337)
```

## Async.resolve()

Manually resolve async result, makes it available on the instance using [`getResult()`](/observables/async.md#async-getresult):

```typescript
import { createAsync } from "@corets/async"

const query = createAsync(async (argument: number) => "some data")

const resultFromValue = await query.resolve("another value")
const resultFromPromise = await query.resolve(Promise.resolve("another value"))
const resultFromProducer = await query.resolve(async () => "another value")
```

## Async.cancel()

Cancel current async invocation:

```typescript
query.cancel()
```

## Async.listen()

Subscribe to state changes, the listener receives the same object that you get from [`getState()`](/observables/async.md#async-getstate):

```typescript
const unsubscribe = query.listen((state) => console.log("async state changed", state))
```


---

# Agent Instructions: 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:

```
GET https://docs.corets.io/observables/async.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
