Return types of asynchronous functions
In asynchronous programming, the async
methods can have three possible return types. These are:
void
Task
Task<TResult>
We will have a look at each return type in the following recipe.
Getting ready
What could be the use of a void
return type in asynchronous methods? Generally, void
is used with event handlers. Just bear in mind that void
returns nothing, so you can't wait for it. Therefore, if you call a void
return type asynchronous method, your calling code should be able to continue executing code without having to wait for the asynchronous method to complete.
With asynchronous methods that have a return type of Task
, you can utilize the await
operator to pause the execution of the current thread until the called asynchronous method has completed. Keep in mind that an asynchronous method that returns a type of Task
basically does not return an operand. Therefore, if it was written as a synchronous method, it would be a void
return type method...