Namespace: FsToolkit.ErrorHandling
Function Signature:
unit -> CancellableTaskValidation<CancellationToken, 'Error>Gets the current CancellationToken from the surrounding cancellableTaskValidation computation expression.
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
}Using the token for an HTTP request with cancellation support:
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
}Sharing the token across multiple operations in a validation workflow:
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
}