等待Task.CompletedTask做什么? [英] await Task.CompletedTask for what?

查看:1232
本文介绍了等待Task.CompletedTask做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Windows Template Studio 是在Build2017上引入的.

I created UWP app with Windows Template Studio that introduced at Build2017.

下面的类是从中生成的代码的一部分.

Below class is a part of generated code from it.

public class SampleModelService
{
    public async Task<IEnumerable<SampleModel>> GetDataAsync()
    {
        await Task.CompletedTask; // <-- what is this for?
        var data = new List<SampleModel>();

        data.Add(new SampleModel
        {
            Title = "Lorem ipsum dolor sit 1",
            Description = "Lorem ipsum dolor sit amet",
            Symbol = Symbol.Globe
        });

        data.Add(new SampleModel
        {
            Title = "Lorem ipsum dolor sit 2",
            Description = "Lorem ipsum dolor sit amet",
            Symbol = Symbol.MusicInfo
        });
        return data;
    }
}

我的问题是,这里await Task.CompletedTask;代码的目的和原因是什么?实际上,它没有Task结果接收者.

My question is, what is the purpose and reason of await Task.CompletedTask; code in here? It actually does not have Task result receiver from it.

推荐答案

通过该方法,可以在以后的阶段更轻松地实现异步代码调用,而不必更改签名,从而避免了重构调用代码.

It is there to make it easier for later stage to implement async code calls without having to alter the signature thus preventing having to refactor the calling code.

虽然脚手架示例代码是同步的,但是Template Studio是专门围绕 async 数据访问层设计的,希望您通过修改所生成方法的主体来实现自己的数据访问./p>

Whilst the scaffolded sample code is synchronous, the Template Studio is designed specifically around an async data access layer, you are expected to implement your own data access by modifying the body of the generated methods.

如果 async 实现是 NOT 实现,则整个模板化应用中的代码都会发生重大变化,这对于新开发人员来说是一个非常陡峭的学习曲线,模板的目的是以最小的努力甚至什至没有经验就能启动并运行!

If the async implementation were NOT implemented, there would be significant code changes throughout the templated app and it would be a very steep learning curve for new developers, the point of the template is to get up and running with minimal effort or even experience!

另一种选择是从方法签名和该行中删除async关键字,然后执行

Another option would be to remove the async keyword from the method signature and that line and do

return Task.FromResult<IEnumerable<SampleModel>>(data); 

当由于接口而需要返回一个等待的Task时,例如在实现没有异步工作要做的时候,您会看到此构造.

You see this construct when you need to return an awaitable Task due to an interface for example while the implementation has no async work to do.

但是,在这种情况下,由于它是模板,因此他们希望人们用await FetchDataFromDatabaseAsync();之类的东西替换await Task.Completed.由于async关键字已经存在,因此可以最小化实现您自己的异步调用所需的更改.

In this case however, since it is a template they expect people to replace the await Task.Completed with something like await FetchDataFromDatabaseAsync();. Since the async keyword is already there it minimizes the needed changes to implement your own async call.

无论如何,没有此等待结构,您可以执行以下操作:

Anyway, without this await construct you can do this:

public class SampleModelService
{
    public Task<IEnumerable<SampleModel>> GetDataAsync()
    {
        var data = new List<SampleModel>();

        data.Add(new SampleModel
        {
            Title = "Lorem ipsum dolor sit 1",
            Description = "Lorem ipsum dolor sit amet",
            Symbol = Symbol.Globe
        });

        data.Add(new SampleModel
        {
            Title = "Lorem ipsum dolor sit 2",
            Description = "Lorem ipsum dolor sit amet",
            Symbol = Symbol.MusicInfo
        });

        return Task.FromResult<IEnumerable<SampleModel>>(data); 
     }
}

如果根本不需要返回Task(您没有任何异步代码),则只需将其完全删除即可. (但是然后您必须重构调用此方法的代码)

If there is no requirement to return a Task at all (you do not have any async code) then just remove it completely. (But then you have to refactor code that calls this method)

查看此代码,我怀疑有人会在开发过程中稍后调用异步方法,并且已经预料到通过指定此方法返回Task.

Reviewing this code I suspect someone is going to call an async method later in the development process and already anticipated that by specifying that this method returns a Task.

这篇关于等待Task.CompletedTask做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆