【问题标题】:Return await fooAsync() or return fooAsync() inside a method [duplicate]在方法中返回 await fooAsync() 或返回 fooAsync() [重复]
【发布时间】:2014-03-27 02:22:01
【问题描述】:

假设我有一个名为fooAsync()的方法:

public Task<T> FooAsync<T>(T foo, T bar)
{
    // do some stuff
    var returnFoo = await netLibraryMethodAsync(x, y, z);
    // do some more stuff
    return returnFoo as T;
}

现在我有一个 awaiter 方法,它只做一件事 call fooAsync()

public Task<string> BarAsync(string foo, string bar)
{
    return this.FooAsync(foo, bar);
}

我的问题是:在 BarAsync 中,我应该使用return await this.FooAsync() 还是return this.FooAsync(),为什么?

我已尝试环顾四周,但没有找到此模式的最终答案。

【问题讨论】:

标签: c# asynchronous async-await


【解决方案1】:

我的问题是:在 BarAsync 中,我应该使用 return await this.FooAsync() 还是 return this.FooAsync() 以及为什么?

您应该使用return FooAsync(foo, bar)。在这种情况下,除了调用另一个异步方法之外,您没有做任何事情,因此通过避免 asyncawait,您可以避免少量开销。

【讨论】:

猜你喜欢
  • 2016-12-12
  • 2014-11-01
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 2020-05-12
相关资源
最近更新 更多