代码之家  ›  专栏  ›  技术社区  ›  t3chb0t

decorator异步方法应该等待内部方法还是只返回它?[复制]

  •  0
  • t3chb0t  · 技术社区  · 7 年前

    这样编写方法的场景:

    public async Task<SomeResult> DoSomethingAsync()
    {
        // Some synchronous code might or might not be here... //
        return await DoAnotherThingAsync();
    }
    

    而不是这样:

    public Task<SomeResult> DoSomethingAsync()
    {
        // Some synchronous code might or might not be here... //
        return DoAnotherThingAsync();
    }
    

    有道理吗?

    为什么要用 return await 当你可以直接返回 Task<T> 从内部 DoAnotherThingAsync() 召唤?

    返回等待 在很多地方,我想我可能错过了一些东西。但据我所知,在本例中不使用async/await关键字,直接返回任务在功能上是等价的。为什么要增加额外的开销 await

    0 回复  |  直到 6 年前