getCancellationToken

CancellableTaskValidation.getCancellationToken

Namespace: FsToolkit.ErrorHandling

Function Signature:

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:

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:

Example 3

Sharing the token across multiple operations in a validation workflow:

Last updated