C# 异步/等待方法到 F#? [英] C# async / await method to F#?

查看:26
本文介绍了C# 异步/等待方法到 F#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 F#,并且正在将一些 C# 代码转换为 F#.

I am trying to learn F# and am in the process of converting some C# code to F#.

我有以下 C# 方法:

I have the following C# method:

public async Task<Foo> GetFooAsync(byte[] content)
{
    using (var stream = new MemoryStream(content))
    {
        return await bar.GetFooAsync(stream);
    }
}

其中 bar 是一些私有字段,GetFooAsync 返回一个 Task.

Where bar is some private field and GetFooAsync returns a Task<Foo>.

这如何转化为 F#?

这是我目前拥有的:

member public this.GetFooAsync (content : byte[]) = 
    use stream = new MemoryStream(content)
    this.bar.GetFooAsync(stream)

返回一个Task.

推荐答案

在 F# 中,异步由 async 计算构建器表示,它不是 Task 的精确模拟>,但通常可以用来代替一个:

In F#, asynchrony is represented by the async computation builder, which is not an exact analog of Task, but can generally be used in place of one:

member public this.GetFooAsync (content : byte[]) = 
   async {
      use stream = new MemoryStream(content) 
      return! this.bar.GetFooAsync(stream) |> Async.AwaitTask
   } 
   |> Async.StartAsTask

这篇关于C# 异步/等待方法到 F#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆