I'm looking at the MSDN documentation and it says
Async methods have three possible return types:
Task<TResult>
,Task
, andvoid
.
https://msdn.microsoft.com/en-us/library/mt674893.aspx
However, I remember reading somewhere else that it's possible to write to await
custom awaiters. How can I reconcile these 2 facts?
await
supports any kind of "awaitable", which are object instances that follow a certain pattern (GetAwaiter
, etc).
async
generates a state machine which only knows how to create Task
/Task<T>
awaitables (or return void
).
So, you can create your own custom awaitable object and return that, but you have to do it explicitly. You can't use async
to do it.
The compiler team is currently looking at adding ValueTask
support to async
. This would allow you to use "async method builders" that would follow a pattern. Then you'd be able to use async
as well as await
to operate on any kind of "tasklike" type. Unfortunately, it would still not allow async
methods to return interface types, though - changes in other parts of the language are necessary for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With