Skip to content

Latest commit

 

History

History
55 lines (41 loc) · 1.64 KB

File metadata and controls

55 lines (41 loc) · 1.64 KB

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:

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:

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
    }