Should I add async/await to a single-line function like:
public async Task<T> GetFoo()
{
return await HandleAsync<T>(....);
}
Or is this unneeded overhead if the parameter does not need an asynchron call and I can simple write:
public Task<T> GetFoo()
{
return HandleAsync<T>(....);
}
Async/Await makes it easier to write promises. The keyword 'async' before a function makes the function return a promise, always. And the keyword await is used inside async functions, which makes the program wait until the Promise resolves.
Yes. If you don't need to wait, don't wait.
There's no specific need to mark a function async unless you specifically need one of the benefits of an async function such as the ability to use await inside that function or the automatic error handling it provides.
C# Language Async-Await Async/await will only improve performance if it allows the machine to do additional work.
Use second overload, because async methods are converted into astate machine behind the scenes (an extra class) to handle asynchronous operation with awaits.
So first method adds unnecessary overhead. Second overload simply returns a task that you can still await on.
Im not sure about how exception handling wold change here but i think it doesnt change.
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