I am trying to learn F# and am in the process of converting some C# code to F#.
I have the following C# method:
public async Task<Foo> GetFooAsync(byte[] content)
{
using (var stream = new MemoryStream(content))
{
return await bar.GetFooAsync(stream);
}
}
Where bar
is some private field and GetFooAsync
returns a Task<Foo>
.
How does this translate to F#?
Here is what I currently have:
member public this.GetFooAsync (content : byte[]) =
use stream = new MemoryStream(content)
this.bar.GetFooAsync(stream)
Which returns a Task
.
In F#, asynchrony is represented by the
async
computation builder, which is not an exact analog ofTask
, but can generally be used in place of one:If you are converting
async
/await
-intensive C# code to F#, it might get cumbersome because of the difference between F#'sasync
andTask
and the fact that you always have to callAsync.AwaitTask
To avoid that you can use FSharpx library, which has a
task
computation expression.The result of
asyncResult
isAsync<string>
and the result oftaskResult
isTask<string>
.