9

I heard that an awaitable is an asynchronous operation. But since it is an important concept why I can't find the precise definition on MSDN?

My question is not how to write async/await. My question is to know the concept. MSDN has the concept async/await but no awaitable.

So what is an awaitable? If it is an operation, what are included?

Bigeyes
  • 1,508
  • 2
  • 23
  • 42
  • 1
    https://msdn.microsoft.com/en-us/library/hh191443(v=vs.110).aspx – ColinM Nov 28 '16 at 20:47
  • Possible duplicate of [How and When to use \`async\` and \`await\`](http://stackoverflow.com/questions/14455293/how-and-when-to-use-async-and-await) – Igor Nov 28 '16 at 20:48
  • 1
    It doesn't have the word `awaitable` in the article. – Bigeyes Nov 28 '16 at 20:48
  • 1
    If we knew where you heard “an awaitable is an asynchronous operation” we might be able to help. – Dour High Arch Nov 28 '16 at 20:58
  • awaitable is the common English term but in .net it is known as async/await (these are the keywords). – CodingYoshi Nov 28 '16 at 20:59
  • @DourHighArch, [Here](http://blog.stephencleary.com/2012/02/async-and-await.html). It can be customized so I think that has some features. Thus I want to know the precise definition. – Bigeyes Nov 28 '16 at 21:01
  • 2
    Read the C# specification section 7.7.7 – Lee Nov 28 '16 at 21:07
  • @Lee. Would you please give me the link? I can't find it by google search. There is no section 7.7.7. – Bigeyes Nov 28 '16 at 21:14
  • Do you have Visual Studio installed? If so you can find the specification at `Program Files (x86)\Microsoft Visual Studio (Version)\VC#\Specifications\1033\CSharp Language Specification.docx`. Or you can download it from [here](https://www.microsoft.com/en-us/download/details.aspx?id=7029&WT.mc_id=rss_alldownloads_all) – Lee Nov 28 '16 at 21:17
  • 2
    In this context, awaitable is class that implements certain GetAwaiter method (or even just has the right extension method with this name) - https://blogs.msdn.microsoft.com/pfxteam/2011/01/13/await-anything/ – Evk Nov 28 '16 at 22:35
  • Simple rule: any method whose name ends in "Async" or returns a Task. – Hans Passant Nov 29 '16 at 14:59
  • Sort of surprised this is not a more popular question. I guess it belongs on softwareengineering.se – Sentinel May 15 '18 at 08:42

2 Answers2

15

Finally I find it on Async/Await FAQ.

An “awaitable” is any type that exposes a GetAwaiter method which returns a valid “awaiter”. This GetAwaiter method may be an instance method (as it is in the case of Task and Task<TResult>), or it may be an extension method.

An “awaiter” is any type returned from an awaitable’s GetAwaiter method and that conforms to a particular pattern. The awaiter must implement the System.Runtime.CompilerServices.INotifyCompletion interface, and optionally may implement the System.Runtime.CompilerServices.ICriticalNotifyCompletion interface. In addition to providing an implementation of the OnCompleted method that comes from INotifyCompletion (and optionally the UnsafeOnCompleted method that comes from ICriticalNotifyCompletion), an awaiter must also provide an IsCompleted Boolean property, as well as a parameterless GetResult method. GetResult returns void if the awaitable represents a void-returning operation, or it returns a TResult if the awaitable represents a TResult-returning operation.

And this link about awaitable and awaiter is helpful.

Community
  • 1
  • 1
Bigeyes
  • 1,508
  • 2
  • 23
  • 42
4

Simply put, an awaitable method or function is one that returns a Task or Task<T>. It doesn't return a data type, instead it returns a process that is asynchronous to the one that is being run at that point in time. If you had two processes, A and B, and B was only allowed to run once A was finished, B would be the awaitable method, because it would wait till A was finished, and only then would it return it's designated Task. Note that an awaitable process can also return void. This isn't the clearest of explanations but definitely helped me when I was learning.