> For the complete documentation index, see [llms.txt](https://demystifyfp.gitbook.io/fstoolkit-errorhandling/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/taskresult/orelsefunctions.md).

# orElse Functions

### TaskResult.orElse

Namespace: `FsToolkit.ErrorHandling`

Returns the original result unchanged if it is `Ok`, otherwise returns the provided alternative result.

### Function Signature:

```fsharp
Task<Result<'ok, 'error2>>
  -> Task<Result<'ok, 'error>>
  -> Task<Result<'ok, 'error2>>
```

### Examples

#### Example 1

```fsharp
TaskResult.error "First"
|> TaskResult.orElse (TaskResult.ok "Second")
// evaluates to Ok "Second"
```

#### Example 2

When the input is already `Ok`, the alternative is ignored:

```fsharp
TaskResult.ok "First"
|> TaskResult.orElse (TaskResult.error "Second")
// evaluates to Ok "First"
```

***

### TaskResult.orElseWith

Namespace: `FsToolkit.ErrorHandling`

Returns the original result unchanged if it is `Ok`, otherwise calls the given function with the error value and returns its result. The alternative function is **not** evaluated unless the input is `Error`.

### Function Signature:

```fsharp
('error -> Task<Result<'ok, 'error2>>)
  -> Task<Result<'ok, 'error>>
  -> Task<Result<'ok, 'error2>>
```

### Examples

#### Example 1

```fsharp
TaskResult.error "First"
|> TaskResult.orElseWith (fun _ -> TaskResult.error "Second")
// evaluates to Error "Second"
```

#### Example 2

Falling back to a secondary data source when the primary fails:

```fsharp
let getUser (id: UserId) : Task<Result<User, string>> =
  fetchFromCache id
  |> TaskResult.orElseWith (fun _ -> fetchFromDatabase id)
```

#### Example 3

Using the error value to decide the fallback strategy:

```fsharp
let result =
  fetchData ()
  |> TaskResult.orElseWith (fun err ->
      if isRetryable err then retryFetch ()
      else TaskResult.error err)
```


---

# 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://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/taskresult/orelsefunctions.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.
