Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Await an async function from setter property

I need to await to an async function from a property setter method.

public String testFunc()
{
get
{

}
set
{
    //Await Call to the async func <asyncFunc()>

}
}

I understand we should not make async properties, so what is the optimal way to do this.

like image 276
Tulika Avatar asked Jan 07 '16 08:01

Tulika


People also ask

Can a property be async?

Just as Swift's functions can be asynchronous, computed properties can also be asynchronous: attempting to access them must also use await or similar, and may also need throws if errors can be thrown when computing the property.

Is async property allowed in C#?

There is no technical reason that async properties are not allowed in C#. It was a purposeful design decision, because "asynchronous properties" is an oxymoron. Properties should return current values; they should not be kicking off background operations.

Can getter be async?

Just return a promise Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. So, a getter that returns a Promise is an async getter.

Can you await a void function?

You use the void return type in asynchronous event handlers, which require a void return type. For methods other than event handlers that don't return a value, you should return a Task instead, because an async method that returns void can't be awaited.


1 Answers

You can't make async properties and you shouldn't want to - properties imply fast, non blocking operations. If you need to perform a long running activity, as implied by you're wanting to kick of an async operation and wait for it, don't make it a property at all.

Remove the setter and make a method instead.

like image 192
Damien_The_Unbeliever Avatar answered Sep 17 '22 15:09

Damien_The_Unbeliever