Skip to content

Commit ceb92d0

Browse files
committed
NoAsyncRunSynchronouslyInLibrary: updated docs
To address feedback.
1 parent 3dd6e70 commit ceb92d0

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

docs/content/how-tos/rules/FL0087.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ hide_menu: true
66

77
# NoAsyncRunSynchronouslyInLibrary (FL0087)
88

9-
*Introduced in `0.26.7`*
9+
*Introduced in `0.26.8`*
1010

1111
## Cause
1212

@@ -19,12 +19,46 @@ The rule assumes the code is in the library if none of the following is true:
1919

2020
## Rationale
2121

22-
Using `Async.RunSynchronously` outside of scripts and tests can lead to program becoming non-responsive.
22+
Using `Async.RunSynchronously` outside of scripts, tests, and console projects can lead to program becoming non-responsive.
2323

2424
## How To Fix
2525

2626
Remove `Async.RunSynchronously` and wrap the code that uses `async` computations in `async` computation, using `let!`, `use!`, `match!`, or `return!` keyword to get the result.
2727

28+
Example:
29+
30+
```fsharp
31+
type SomeType() =
32+
member self.SomeMethod someParam =
33+
let foo =
34+
asyncSomeFunc someParam
35+
|> Async.RunSynchronously
36+
processFoo foo
37+
```
38+
39+
The function can be modified to be asynchronous. In that case it might be better to prefix its name with Async:
40+
41+
```fsharp
42+
type SomeType() =
43+
member self.AsyncSomeMethod someParam = async {
44+
let! foo = asyncSomeFunc someParam
45+
return processFoo foo
46+
}
47+
```
48+
49+
In case the method/function is public, a nice C#-friendly overload that returns `Task<'T>` could be provided, suffixed with Async, that just calls the previous method with `Async.StartAsTask`:
50+
51+
```fsharp
52+
type SomeType() =
53+
member self.AsyncSomeMethod someParam = async {
54+
let! foo = asyncSomeFunc someParam
55+
return processFoo foo
56+
}
57+
member self.SomeMethodsync someParam =
58+
self.AsyncSomeMethod someParam
59+
|> Async.StartAsTask
60+
```
61+
2862
## Rule Settings
2963

3064
{

0 commit comments

Comments
 (0)