简体   繁体   中英

Should I not use await/async when returning Task?

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文简体   中文繁体   中英对照 版本,有任何建议请联系yoyou2525@163.com。

When I want to create a method returning Task with I/O bound work I usually use something like this:

public async Task<ProductModel> GetProduct(string name, CancellationToken ct = default)
{
    //...
    return await GetAsync<ProductModel>(request, ct);
}

And call it like this:

var product = await GetProduct("some product", ct);

But I came across some function definition just like above but without the await/async :

public Task<ProductModel> GetProduct(string name, CancellationToken ct = default)
{
    //...
    return GetAsync<ProductModel>(request, ct);
}

With a similar call as mine.

My question is, is this latter definition safe, can I just suppress the await/async in the function returning a Task ?

And if so, are there any benefits or drawbacks?

No answers.You can refer to the related questions on the right.
8 Use async await or task?

You have built a complex calculation algorithm. It takes quite some time to complete and you want to make sure that your application remains responsiv ...

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM