Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is awaitable?

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?

like image 486
Bigeyes Avatar asked Nov 28 '16 20:11

Bigeyes


People also ask

What is Awaitable Python?

An awaitable object is an object that defines __await__() method returning an iterator. Not much to add here. Just return an iterator from that method. The only thing you need to understand is how does it work. I mean, how asyncio or another similar framework achieves concurrency in a single thread.

What does await actually do?

The await operator suspends evaluation of the enclosing async method until the asynchronous operation represented by its operand completes. When the asynchronous operation completes, the await operator returns the result of the operation, if any.

What is the purpose of async?

Note: The purpose of async / await is to simplify the syntax necessary to consume promise-based APIs. The behavior of async / await is similar to combining generators and promises. Async functions always return a promise.

What is the benefit of async await?

A significant benefit of the async/await pattern in languages that support it is that asynchronous, non-blocking code can be written, with minimal overhead, and looking almost like traditional synchronous, blocking code.


1 Answers

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.

like image 14
Bigeyes Avatar answered Oct 11 '22 22:10

Bigeyes