2

Suppose I have a method called 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;
}

Now I have an awaiter method that does only one thing call fooAsync().

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

My question here is that: inside BarAsync, should I use return await this.FooAsync() or return this.FooAsync() and why?

I have tried looking around but haven't found a conclusive answer for this pattern.

Tu Hoang
  • 4,622
  • 13
  • 35
  • 48
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 27 '14 at 02:23
  • @JohnSaunders: Understood sir. – Tu Hoang Mar 27 '14 at 02:26
  • Wouldn't return await FooAsync(foo, bar); have return type T? – bit Mar 27 '14 at 04:32
  • @bit: Yeah, it was a quick sample. But the question remains valid :) – Tu Hoang Mar 27 '14 at 05:15
  • 2
    I think you should return this.FooAsync(foo, bar); and leave it upto the caller, whether to await or not. – bit Mar 27 '14 at 05:17
  • 1
    They are basically the same (unless there are other awaits in your BarAsync before the call to FooAsync). – Aron Mar 27 '14 at 05:23
  • 2
    also possible duplicate of [Any difference between “await Task.Run(); return;” and “return Task.Run()”?](http://stackoverflow.com/q/21033150/1768303) – noseratio Mar 27 '14 at 06:46

1 Answers1

2

My question here is that: inside BarAsync, should I use return await this.FooAsync() or return this.FooAsync() and why?

You should use return FooAsync(foo, bar). In this case, you're not doing anything other than calling another asynchronous method, so by avoiding async and await, you're avoiding a small amount of overhead.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • 1
    I have bumped into so many articles of yours in my quest of understanding async/await :) Also, we should also mention this article: http://msdn.microsoft.com/en-us/magazine/hh456402.aspx – Tu Hoang Mar 27 '14 at 19:57