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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With