getCancellationToken

CancellableTaskResult.getCancellationToken

Namespace: FsToolkit.ErrorHandling

Function Signature:

unit -> CancellableTaskResult<CancellationToken, 'Error>

Gets the current CancellationToken from the surrounding cancellableTaskResult computation expression.

Examples

Note: Many use-cases requiring the cancellation token can also be solved using the cancellableTaskResult computation expression.

Example 1

Passing the cancellation token to a cancellable database operation:

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

Example 2

Using the token to cancel an HTTP request:

Example 3

Passing the token to multiple operations within the same computation:

Last updated