Skip to content

Commit

Permalink
feat/async_v2: revamping the Asyncronnous Code section
Browse files Browse the repository at this point in the history
  • Loading branch information
⚙︎ Greg committed Jul 8, 2024
1 parent d07a09c commit 4b8dc88
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions docs/fsharp-cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ In F#, .NET Tasks can be constructed using the `task { }` computational expressi

### Asynchronous Workflows

Asynchronous workflows were invented before .NET Tasks existed, which is why F# has two core methods for asynchronous programming. However, Asynchronous Workflows did not become obsolete. They offer another, but different, approach: dataflow programming.
Asynchronous workflows were invented before .NET Tasks existed, which is why F# has two core methods for asynchronous programming. However, Asynchronous Workflows did not become obsolete. They offer another, but different, approach: dataflow.
Asynchronous blocks are constructed using the `async { }` expression. Then using the [`Async` library](https://fsharp.github.io/fsharp-core-docs/reference/fsharp-control-fsharpasync.html#section3) the blocks are composed and executed.
In contrast to .NET Tasks, asynchronous workflows are "cold" - need to be explicitly started - and every workflow passes a CancellationToken implicitly.

Expand Down Expand Up @@ -846,7 +846,7 @@ If canceled, the computation will cancel any remaining child computations but wi
for cnt in [ 0 .. 10 ] do
token.ThrowIfCancelled()
printf $"{cnt}: And..."
do! Task.Delay ((TimeSpan.FromSeconds 1), token) // token is essential here; otherwise "Done" will be printed after cts.Cancel()
do! Task.Delay ((TimeSpan.FromSeconds 1), token) // token is required here; otherwise "Done" will be printed before cancelling
printfn "Done"
}

Expand All @@ -866,7 +866,7 @@ or one is created for you; accessed via `Async.DefaultCancellationToken`
printf $"{cnt}: And..."
do! Async.Sleep (TimeSpan.FromSeconds 1) // Async.Sleep implicitly receives the Cancellation Token

let! ct = Async.CancellationToken // when interoping with Tasks, they need to be passed explicitly
let! ct = Async.CancellationToken // when interoping with Tasks, cancellationTokens need to be passed explicitly
do! Task.Delay ((TimeSpan.FromSecond 1), cancellationToken = ct) |> Async.AwaitTask

printfn "Done"
Expand All @@ -882,7 +882,8 @@ All methods for cancellation can be found in the [Core Library Documentation](ht
Asynchronous programming is a vast topic. Here are some other resources worth exploring:

- [Iced Tasks](https://github.com/TheAngryByrd/IcedTasks?tab=readme-ov-file#icedtasks) - .NET Tasks start immediately. The IcedTasks library provide additional [computational expressions](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions) that combine the benefits of .NET Tasks (interop and performance) with asynchronous workflows (composability and multi-start).
- [Async Guide](https://medium.com/@eulerfx/f-async-guide-eb3c8a2d180a) by Leo Gorodinski - An in-depth guide to `Async` including hazards to avoid and under-the-hood explanations.
- [F# Async Guide](https://medium.com/@eulerfx/f-async-guide-eb3c8a2d180a) by Leo Gorodinski - An in-depth guide to `Async` including hazards to avoid and under-the-hood explanations.
- [Asynchronous Programming Best Practices](https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#table-of-contents) by David Fowler - offers a fantastic list of good practices for .NET Task usage.

<div id="code-organization"></div>

Expand Down

0 comments on commit 4b8dc88

Please sign in to comment.