# Computation Expression

### AsyncResult Computation Expression

Namespace: `FsToolkit.ErrorHandling`

### Examples

#### Example 1

The initial [motivating example](https://github.com/demystifyfp/FsToolkit.ErrorHandling/blob/master/README.md) is perhaps more realistic if some functions are asynchronous. Given the following functions:

```fsharp
tryGetUser : string -> Async<User option>
isPwdValid : string -> User -> bool
authorize : User -> Async<Result<unit, AuthError>>
createAuthToken : User -> Result<AuthToken, TokenError>
```

A login flow can be implemented as below using the `asyncResult` CE and a few [helpers](broken://pages/-LX7mJxjsGPNe9B0tzfw):

```fsharp
type LoginError = 
	| InvalidUser
	| InvalidPwd
	| Unauthorized of AuthError
	| TokenErr of TokenError

let login (username: string) (password: string) : Async<Result<AuthToken, LoginError>> =
  asyncResult {
    let! user = username |> tryGetUser |> AsyncResult.requireSome InvalidUser
    do! user |> isPwdValid password |> Result.requireTrue InvalidPwd
    do! user |> authorize |> AsyncResult.mapError Unauthorized
    return! user |> createAuthToken |> Result.mapError TokenErr
  }
```


---

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