Chaining two functions () -> Task and A->Task

前端 未结 4 1680
你的背包
你的背包 2021-02-08 13:33

I don\'t know if I am thinking in the wrong way about TPL, but I have difficulty understanding how to obtain the following:

I have two functions

Task<         


        
4条回答
  •  轮回少年
    2021-02-08 13:55

    If you are unable to use await, you can certainly use Unwrap, but it handles exceptions sub-optimally. The method I prefer is Then as described in this article. Composition becomes simple and elegant:

    Task Combined()
    {
      return getA().Then(getB);
    }
    

    For those interested in the details, I wrote a blog post a while ago about exactly this problem of composing asynchronous methods, and how monads provide an elegant solution.

提交回复
热议问题