为什么从包含等待的方法中使用

为什么从包含等待的方法中使用

本文介绍了为什么从包含等待的方法中使用 Task.FromResult<T>(T result)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在教程中遇到了以下方法;

I came across the following method in a tutorial;

    private async Task<ClaimsIdentity> GetClaimsIdentity(string userName, string password)
    {
        if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            return await Task.FromResult<ClaimsIdentity>(null);

        // get the user to verifty
        var userToVerify = await _userManager.FindByNameAsync(userName);

        if (userToVerify == null) return await Task.FromResult<ClaimsIdentity>(null);

        // check the credentials
        if (await _userManager.CheckPasswordAsync(userToVerify, password))
        {
            return await Task.FromResult(_jwtFactory.GenerateClaimsIdentity(userName, userToVerify.Id));
        }

        // Credentials are invalid, or account doesn't exist
        return await Task.FromResult<ClaimsIdentity>(null);
    }

作者总是使用 await Task.FromResult(...) 即使返回 null.我不是 Task-await 模式的专家,我会编写这样的方法;

The author always uses await Task.FromResult<ClaimsIdentity>(...) even when returning null. I'm no expert in the Task-await pattern and would have written the method something like this;

    private async Task<ClaimsIdentity> GetClaimsIdentity(string userName, string password)
    {
        if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            return null;

        // get the user to verifty
        var userToVerify = await _userManager.FindByNameAsync(userName);

        if (userToVerify == null) return null;

        // check the credentials
        if (await _userManager.CheckPasswordAsync(userToVerify, password))
        {
            return _jwtFactory.GenerateClaimsIdentity(userName, userToVerify.Id);
        }

        // Credentials are invalid, or account doesn't exist
        return null;
    }

两者都编译.这两种方法有什么区别(如果有的话)?以这种方式使用 await Task.FromResult(null) 有什么好处吗?

Both compile. What is the difference (if any) between these two methods? Is there anything to be gained by using await Task.FromResult<ClaimsIdentity>(null) in this manner?

推荐答案

根据我找到的关于 Task.FromResult 的最佳 stackoverflow 答案:https://stackoverflow.com/a/19696462/6440521

According to the best stackoverflow answer I've found about Task.FromResult: https://stackoverflow.com/a/19696462/6440521

Task.FromResult 的用法仅适用于同步方法和模拟的上下文.因此,当您只想返回结果时,在 async 方法中使用它是多余的 - 不会给您带来额外的好处,而且 AsyncGuidance 也没有说明使用 Task.FromResultasync 方法中:https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md

The usage of Task.FromResult is appropriate only in the context of synchronous methods and mocking. So using it in an async method when you just want to return a result is redundant - gives you no additional benefits, also AsyncGuidance does not say anything about using Task.FromResult in an async method:https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md

因此,在 async 方法中使用 Task.FromResult 的 AFAIK 是不必要的,它会使您的代码变得臃肿并且不会给您带来真正的好处.

So AFAIK using Task.FromResult in an async method is unnecessary, bloats your code and gives you no real benefits.

这篇关于为什么从包含等待的方法中使用 Task.FromResult&lt;T&gt;(T result)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:18