[英]Is it right to call (label) a method returning a task as async
I have methods provided by the database driver that pushes a value to db (both async and non-async) like so,我有数据库驱动程序提供的方法,可以将值推送到 db(异步和非异步),如下所示,
pushToDBAsync();
and pushToDB();
和
pushToDB();
So I created a wrapper method for the driver methods for my own implementation shown below.所以我为我自己的实现的驱动程序方法创建了一个包装方法,如下所示。 Is this correct?
这样对吗? and also Is it right to call this an async method?
并且将其称为异步方法是否正确?
public Task<bool> pushToDBAsync(.....)
{
return _database.pushToDBAsync(.....);
}
This is an async method as the caller can await it.这是一个异步方法,因为调用者可以等待它。 The difference with using
与使用的区别
public async Task<bool> pushToDBAsync(....)
is that you give the caller the capability to decide how to handle the Task.是您赋予调用者决定如何处理任务的能力。 Doing in that way may also help avoiding some pitfalls as mentioned in this blogpost: http://blog.stephencleary.com/2016/12/eliding-async-await.html
这样做也可能有助于避免本博文中提到的一些陷阱: http : //blog.stephencleary.com/2016/12/eliding-async-await.html
Long story short: Labeling such a method with Async communicates the caller that it can be awaited and makes it easier to interact with your code.长话短说:用 Async 标记这样的方法可以让调用者知道它可以被等待,并且可以更轻松地与您的代码进行交互。
*If you want to use your method as a pushToDBAsync(); *如果您想将您的方法用作 pushToDBAsync(); so you follow below code for method syntax.
所以你按照下面的代码来了解方法语法。
public Async Task<bool> pushToDBAsync(.....)
{
return await _database.pushToDBAsync(.....);
}
*If you want to use your method as a pushToDB(); *如果你想使用你的方法作为 pushToDB(); So you follow below code for method syntax.
所以你遵循下面的方法语法代码。
public Task<bool> pushToDBAsync(.....)
{
return _database.pushToDBAsync(.....);
// Return same but execution time more compare than Async method.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.