Let's say I want to call, from F#, this C# function:
public static class Foo {
public Bar Baz()
{
...
}
}
The problem is this function is CPU intensive and I don't want to block on it. Unfortunately the C# library doesn't have an Task<Bar> BazAsync()
overload.
Then I want to provide the asynchronous version myself, by creating an F# function that calls it and returns an (already started) Async<Bar>
. That is, I don't want to use System.Threading.Task<Bar>
.
I guess what I'm looking for is the equivalent of Task<T>.Run()
in the F# Async way of doing things.
I've already looked at the following options:
Async.StartAsTask
-> deals with C#ish Task type.Async.Start
andAsync.StartImmediately
-> receiveAsync<unit>
notAsync<T>
Is Async.StartChild
what I'm looking for?
If yes, it would be:
let BazWrapper() =
let asyncTask: Async<Bar> = async {
return Foo.Bar()
}
Async.StartChild asyncTask
However, if this above is the solution:
- Why most documentation around F# async workflows doesn't mention StartChild?
- Why BazWrapper returns
Async<Async<Bar>>
instead ofAsync<Bar>
?