You can only await an async method. However that async method itself needs to have an await statement inside it itself. Doesn't this lead to an infinite regression?
You can only await an async method.
That's not true. You can await anything that follows the "awaitable pattern" (something with a GetAwaiter()
method, the return type of which has appropriate IsCompleted
, GetResult()
, and OnCompleted()
members.
Note that even when you're awaiting the result of calling an async method, it really is the result that you're awaiting... and it doesn't matter that the result happened to be from an async method. As far as the compiler's concerned, you're just awaiting a Task
or a Task<T>
, which was the result of a method call. The async
part only matters within the implementation of the method.
You can await other things as well though - for example, Task.Yield
returns a YieldAwaitable
which isn't a task, but which implements the awaitable pattern. You can implement the awaitable pattern yourself (and cause all kinds of fun mayhem by doing so, should you wish to).
However that async method itself needs to have an await statement inside it itself.
Again, not true - although the compiler will warn you if you don't have an await expression within an async method, as that's almost always a sign that you're doing something wrong.
See more on this question at Stackoverflow