I have the following async method:
public async Task<Result> Create(ModelStateDictionary modelState, TCreateViewModel postedModel)
{
var result = new Result();
if (! modelState.IsValid)
{
result.CopyErrorsFromModelState(modelState);
return result;
}
// never reached if modelState is invalid
var attemptCreateResult = await AttemptCreate(dataModel);
result.MergeResult(attemptCreateResult);
return result;
}
Say I changed it to the below. (I know it's kind of silly, but it's just to demonstrate.)
public async Task<Result> Create(ModelStateDictionary modelState, TCreateViewModel postedModel)
{
var result = new Result();
if (! modelState.IsValid)
{
result.CopyErrorsFromModelState(modelState);
}
return await Task.FromResult(result);
}
If ModelState
is not valid, it would follow the exact same code path.
Yet on the first, I can return the result
directly. And in the second I would be required to wrap my return result
in Task.FromResult()
.
Can somebody explain to me why this is?
NOTE: I've really struggled to make this question clear, and not sure I've succeeded. I'd welcome any edits to clarify it. Thanks.