The return type of an async method must be void, Task or Task<T>
Solution 1
Change the return type to Task<Dictionary<string, float>>
:
public async Task<Dictionary<string, float>> GetLikelihoodsAsync(List<string> inputs)
you can also replace your usage of ContinueWith
to use await
:
var response = await GetResponseAsync(requestData, client, uri);
var result = await ParseResponseAsync(response);
return result;
Solution 2
As stated in the error:
The return type of an async method must be
void
,Task
orTask<T>
In your case that would be Task<T>
, or specifically Task<Dictionary<string, float>>
. So, your method needs to be declared as this:
public async Task<Dictionary<string, float>> GetLikelihoodsAsync(List<string> inputs)
Note that you don't actually need to return a Task<T>
, you only need to return T
. You can read more about it here.
Tamas Ionut
Updated on July 09, 2022Comments
-
Tamas Ionut almost 2 years
I have the following code here:
public async Dictionary<string, float> GetLikelihoodsAsync(List<string> inputs) { HttpClient client = new HttpClient(); string uri = GetUri(); string body = GetRequestBody(inputs); byte[] requestData = Encoding.UTF8.GetBytes(body); Dictionary<string, float> result = await GetResponseAsync(requestData, client, uri) .ContinueWith(responseTask => ParseResponseAsync(responseTask.Result)) .ContinueWith(task => task.Result.Result); return result; }
with
async Task<HttpResponseMessage> GetResponseAsync(byte[] requestData, HttpClient client, string uri) {...} async Task<Dictionary<string, float>> ParseResponseAsync(HttpResponseMessage response) {...}
Basically after GetResponseAsync completes I want to take the results and feed it to ParseResponseAsync and get a task with the results of it.
Currently, it gives an compiler error saying
The return type of an async method must be void, Task or Task
What is the best way to achieve this goal and get rid of this error? Other (better solutions) are welcomed and some explanations of why do task.Result.Result in the last ContinueWith are also welcomed.
-
Tamas Ionut about 8 yearsAnd the two awaits have the same semantic as the ContinueWith?
-
Servy about 8 years@TamasIonut They propagate exceptions properly, rather than just swallowing them. Other than that they're the same.
-
Tamas Ionut about 8 years@Servy So why then having the extra ContinueWith API?
-
Lee about 8 years@TamasIonut - The
Task
classes were added in .net 4, which coincided with the release C#4.async/await
was added later in C#5, and effectively provides a way for the compiler to write the continuations for you. -
Servy about 8 years@TamasIonut 1. Not all problems can be solved with
await
. There will be times, albeit rarely, where it's needed 2. It predatesawait
by a full language version 3.await
simply refactors the code into calls toContinueWith
; it's necessary for it to be there as that's what allows it to work. -
Tamas Ionut about 8 years@Servy By those rare times, you mean situations where better cancellation or context support is needed?
-
Servy about 8 years@TamasIonut I've never come across a situation where I wanted cancellation or context behavior that couldn't be more easily implemented using
async
, but I suppose it's possible for someone to be in such a situation. Typically I see it used to add trivial continuations where extracting the code out into anasync
method just isn't worth it, even though it would work. -
Tamas Ionut about 8 yearsThanks for the answer but the previous one is a bit more complete, since it provides a better way for ContinueWith API.