### Adding IcedTasks NuGet Package Source: https://theangrybyrd.github.io/IcedTasks Install the IcedTasks library using the .NET CLI by adding the NuGet package. ```bash dotnet add nuget IcedTasks ``` -------------------------------- ### Executing CancellableTask Source: https://theangrybyrd.github.io/IcedTasks This example shows how to execute a `CancellableTask` by providing a `CancellationToken`. In ASP.NET, `httpContext.RequestAborted` would be appropriate. ```F# let executeWriting = task { // CancellableTask is an alias for `CancellationToken -> Task<_>` so we'll need to pass in a `CancellationToken`. // For this example we'll use a `CancellationTokenSource` but if you were using something like ASP.NET, passing in `httpContext.RequestAborted` would be appropriate. use cts = new CancellationTokenSource() // call writeJunkToFile from our previous example do! writeJunkToFile cts.Token } ``` -------------------------------- ### Accessing CancellationToken with CancellableTask (getCancellationToken) Source: https://theangrybyrd.github.io/IcedTasks Bind against `CancellableTask.getCancellationToken` to get the current context's `CancellationToken`. This is useful when the async operation does not directly accept a `CancellationToken`. ```F# let writeJunkToFile = let path = Path.GetTempFileName() cancellableTask { let junk = Array.zeroCreate bufferSize use file = File.Create(path) // You can bind against `CancellableTask.getCancellationToken` to get the current context's `CancellationToken`. let! ct = CancellableTask.getCancellationToken () for i = 1 to manyIterations do do! file.WriteAsync(junk, 0, junk.Length, ct) ``` -------------------------------- ### Awaiting Tasks in AsyncEx Source: https://theangrybyrd.github.io/IcedTasks Shows how to use let! and do! with various awaitable types like Task, ValueTask, and YieldAwaitable inside asyncEx. ```fsharp open IcedTasks let myAsyncEx = asyncEx { let! _ = task { return 42 } // Task let! _ = valueTask { return 42 } // ValueTask let! _ = Task.Yield() // YieldAwaitable return 42 } ``` -------------------------------- ### Handling Exceptions in AsyncEx Source: https://theangrybyrd.github.io/IcedTasks Illustrates how AsyncEx unwraps Task exceptions to avoid AggregateException, allowing direct catching of the original exception. ```fsharp let data = "lol" let inner = asyncEx { do! task { do! Task.Yield() raise (ArgumentException "foo") return data } :> Task } let outer = asyncEx { try do! inner return () with | :? ArgumentException -> // Should be this exception and not AggregationException return () | ex -> return raise (Exception("Should not throw this type of exception", ex)) } ``` -------------------------------- ### Using IAsyncDisposable in AsyncEx Source: https://theangrybyrd.github.io/IcedTasks Demonstrates how to use the 'use' keyword with IAsyncDisposable within an asyncEx computation expression. ```fsharp open IcedTasks let fakeDisposable () = { new IAsyncDisposable with member __.DisposeAsync() = ValueTask.CompletedTask } let myAsyncEx = asyncEx { use _ = fakeDisposable () return 42 } ``` -------------------------------- ### Lazy Execution with ColdTask Source: https://theangrybyrd.github.io/IcedTasks Demonstrates that ColdTasks do not execute until they are explicitly called, similar to F# Async. ```fsharp open IcedTasks let coldTask_dont_start_immediately = task { let mutable someValue = null let fooColdTask = coldTask { someValue <- 42 } do! Async.Sleep(100) // ColdTasks will not execute until they are called, similar to how Async works Expect.equal someValue null "" // Calling fooColdTask will start to execute it do! fooColdTask () Expect.equal someValue 42 "" } ``` -------------------------------- ### Using ValueTask Computation Expression Source: https://theangrybyrd.github.io/IcedTasks Shows the usage of the valueTask computation expression to await a ValueTask. ```fsharp open IcedTasks let myValueTask = task { let! theAnswer = valueTask { return 42 } return theAnswer } ``` -------------------------------- ### Iterating IAsyncEnumerable in AsyncEx Source: https://theangrybyrd.github.io/IcedTasks Demonstrates using the 'for' keyword to iterate over an IAsyncEnumerable within an asyncEx block. ```fsharp open IcedTasks open FSharp.Control let myAsyncEx = asyncEx { let items = taskSeq { // IAsyncEnumerable yield 42 do! Task.Delay(100) yield 1701 } let mutable sum = 0 for i in items do sum <- sum + i return sum } ``` -------------------------------- ### Accessing CancellationToken with CancellableTask (Binding) Source: https://theangrybyrd.github.io/IcedTasks Use `do!` directly against a function with the signature `CancellationToken -> Task<_>` to access the context's `CancellationToken`. This is slightly more performant. ```F# let writeJunkToFile = let path = Path.GetTempFileName() cancellableTask { let junk = Array.zeroCreate bufferSize use file = File.Create(path) for i = 1 to manyIterations do // You can do! directly against a function with the signature of `CancellationToken -> Task<_>` to access the context's `CancellationToken`. This is slightly more performant. do! fun ct -> file.WriteAsync(junk, 0, junk.Length, ct) ``` -------------------------------- ### Executing Multiple Asyncs in Parallel with parallelAsync Source: https://theangrybyrd.github.io/IcedTasks Use `parallelAsync` to execute multiple asynchronous operations concurrently and wait for all of them to complete. This is useful for I/O-bound operations like network requests. ```F# open IcedTasks let exampleHttpCall url = async { // Pretend we're executing an HttpClient call return 42 } let getDataFromAFewSites = parallelAsync { let! result1 = exampleHttpCall "howManyPlantsDoIOwn" and! result2 = exampleHttpCall "whatsTheTemperature" and! result3 = exampleHttpCall "whereIsMyPhone" // Do something meaningful with results return () ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.