Reputation: 11264
Saw kind of this code in production:
var task = new HttpClient().GetAsync(u);
var response = await task;
if (task.IsCompletedSuccessfully)
{
Console.WriteLine($"Task is faulted: {task}");
}
Question: Does it make any sense to check the Task state after the await
keyword? As far as I know, the compiler will build a state-machine "around" this code, which throws an exception in case of an error. Based on that it won't make any sense to check the Tasks state.
Am I missing something?
Thanks
Upvotes: 3
Views: 2325
Reputation: 62101
Nope.
If there is an exception, then await will throw it. It wil lbasically not return the task, but the return value or throw the exception.
As such, there is no sense in evaluating the task further.
Otherwise the gain from await would be quite insignificant ;)
Upvotes: 6