# getCancellationToken

### CancellableTaskValidation.getCancellationToken

Namespace: `FsToolkit.ErrorHandling`

Function Signature:

```fsharp
unit -> CancellableTaskValidation<CancellationToken, 'Error>
```

Gets the current `CancellationToken` from the surrounding `cancellableTaskValidation` computation expression.

### Examples

#### Example 1

Passing the cancellation token to a cancellable database call:

```fsharp
let fetchUser (userId: UserId) : CancellableTaskValidation<User, string> =
    cancellableTaskValidation {
        let! ct = CancellableTaskValidation.getCancellationToken()
        let! user = db.Users.FindAsync(userId, ct) |> Task.map (Result.requireSome ["User not found"])
        return user
    }
```

#### Example 2

Using the token for an HTTP request with cancellation support:

```fsharp
let downloadContent (url: string) : CancellableTaskValidation<string, string> =
    cancellableTaskValidation {
        let! ct = CancellableTaskValidation.getCancellationToken()
        let! response = httpClient.GetAsync(url, ct) |> Task.mapValidation id
        let! content = response.Content.ReadAsStringAsync(ct) |> Task.mapValidation id
        return content
    }
```

#### Example 3

Sharing the token across multiple operations in a validation workflow:

```fsharp
let validateAndSaveOrder (order: Order) : CancellableTaskValidation<OrderId, string> =
    cancellableTaskValidation {
        let! ct = CancellableTaskValidation.getCancellationToken()
        let! validatedItems = checkInventory order.Items ct
        let! validatedAddress = verifyAddress order.Address ct
        let! orderId = saveOrder validatedItems validatedAddress ct
        return orderId
    }
```


---

# 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.icedtasks/index-1/getcancellationtoken.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.
