> ## Documentation Index
> Fetch the complete documentation index at: https://acme-c84a37e5.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Result Type

> Discriminated result for success or error operations, with optional metrics

## Overview

The library functions return a result that avoids exceptions and facilitates flow control.

* `run` returns an object with `ok: true` and `data`, or `ok: false` and `error`.
* `all` returns an array with results per task: `ok`, `error` or `skipped`.
* Optional metrics help observe attempts, retries, and duration.

## `RunResult` in `run`

```typescript theme={null}
import { run } from "tryo";

type User = { id: string; name: string };

const result = await run<User>(() => fetch("/api/user").then((r) => r.json()));

if (result.ok) {
  console.log(result.data);
} else {
  console.error(result.error.code);
}
```

Result structure:

* Success: `{ ok: true, data, error: null, metrics? }`
* Error: `{ ok: false, data: null, error, metrics? }`

Metrics include:

* `totalAttempts`
* `totalRetries`
* `totalDuration`
* `lastError?`

## `RunAllItemResult` in `all`

```typescript theme={null}
import { all } from "tryo";

const tasks = [
  () => fetch("/a").then((r) => r.text()),
  () => fetch("/b").then((r) => r.text()),
  () => fetch("/c").then((r) => r.text()),
];

const results = await all<string>(tasks);

for (const r of results) {
  if (r.status === "ok") console.log(r.data);
  if (r.status === "error") console.error(r.error.code);
}
```

Possible states per item:

* `ok`: successful task with `data`
* `error`: failed task with `error`
* `skipped`: task not rund due to cancellation or fail-fast

## Type helpers for `all`

The library exports utilities to discriminate results and improve typing:

```typescript theme={null}
import { isSuccess, type SuccessResult, type ErrorResult } from "tryo";

const results = await all<number>([
  async () => 1,
  async () => {
    throw new Error("x");
  },
]);

const successes: SuccessResult<number>[] = results.filter(isSuccess);
const failures: ErrorResult<any>[] = results.filter(
  (r) => r.status === "error"
);
```
