When is the best place to use Task.Result instead of awaiting Task

前端 未结 4 691
小鲜肉
小鲜肉 2020-12-31 19:09

Whilst I\'ve been using async code in .NET for a while, I\'ve only recently started to research it and understand what\'s going on. I\'ve just been going through my code an

4条回答
  •  甜味超标
    2020-12-31 19:55

    In your case, you can use:

    user = await _userRepo.UpdateLastAccessed(await userTask, DateTime.Now);
    

    or perhaps more clearly:

    var user = await _userRepo.GetByUsername(User.Identity.Name);
    //Some work that doesn't rely on the user object
    user = await _userRepo.UpdateLastAccessed(user, DateTime.Now);
    

    The only time you should touch .Result is when you know the task has been completed. This can be useful in some scenarios where you are trying to avoid creating an async state machine and you think there's a good chance that the task has completed synchronously (perhaps using a local function for the async case), or if you're using callbacks rather than async/await, and you're inside the callback.

    As an example of avoiding a state machine:

    ValueTask FetchAndProcess(SomeArgs args) {
        async ValueTask Awaited(ValueTask task) => SomeOtherProcessing(await task);
        var task = GetAsyncData(args);
        if (!task.IsCompletedSuccessfully) return Awaited(task);
        return new ValueTask(SomeOtherProcessing(task.Result));
    }
    

    The point here is that if GetAsyncData returns a synchronously completed result, we completely avoid all the async machinery.

提交回复
热议问题