Comments (4)
Here is guidance from David Fowler.
Prefer
async
/await
over directly returningTask
There are benefits to using the
async
/await
keyword instead of directly returning theTask
:
- Asynchronous and synchronous exceptions are normalized to always be asynchronous.
- The code is easier to modify (consider adding a
using
, for example).- Diagnostics of asynchronous methods are easier (debugging hangs etc).
- Exceptions thrown will be automatically wrapped in the returned
Task
instead of surprising the caller with an actual exception.❌ BAD This example directly returns the
Task
to the caller.public Task<int> DoSomethingAsync() { return CallDependencyAsync(); }✅ GOOD This examples uses async/await instead of directly returning the Task.
public async Task<int> DoSomethingAsync() { return await CallDependencyAsync(); }
from asyncfixer.
Personally, I think our team would prefer an analyzer that does the opposite of AsyncFixer01: Warn whenever someone directly returns a task instead of awaiting it 🙂
Perhaps it's a different between application authors and library authors: Application authors probably prefer to await before returning, so they can debug more easily, while library authors may sometimes want to directly return without awaiting.
from asyncfixer.
I agree that AsyncFixer01 should not be treated as an anti-pattern.
In Stephen Cleary's blog, there is a post Eliding Async and Await that states, that the performance gain by eliding is minimal, and that the pitfalls for eliding are much greater than the performance gain.
He presents the pitfalls in the post and also presents his recommended guidelines:
- Do not elide by default. Use the
async
andawait
for natural, easy-to-read code. - Do consider eliding when the method is just a passthrough or overload.
So based on this, the AsyncFixer01 should not be considered a must do fix, but a suggestion to consider.
from asyncfixer.
I miss a clear explanation for the AyncFixer01. Except for the argument that has a performance impact, I see no arguments. That is lacking for this tool, it is a kind of recipe book but it does not explain when the recipe is valid and when it is better to solve the issue into another way. I really would like a tutorial, not only with the do not examples, but also with examples the other way round.
from asyncfixer.
Related Issues (20)
- Using declaration treated differently to using blocks HOT 1
- Visual Studio 2022 support HOT 5
- AsyncFixer01 does not take await foreach into account HOT 1
- VS2022 Support HOT 7
- Analyzer links should be directed to the docs HOT 1
- AsyncFixer01: false positive with ValueTask awaited in method returning Task HOT 4
- `delegate Task FooAsync()` is passed a `Func<Task<T>>` hiding a bug
- AsyncFixer05 doesn't cover `return Task.Factory.StartNew(async () =>...);`
- CancellationTokenSource in using block inconsistently triggers AsyncFixer04
- AsyncFixer04 should also include return of Task HOT 1
- nameof(Task<object>.Result) should not trigger error AsyncFixer02
- AsyncFixer05 of TaskTupleAwaiter
- asyncfixer.com down
- Conflicting AsyncFixer01 on use of FluentAssertions ThrowAsync HOT 5
- provide as dotnet tool
- What's the preferred way to fix AsyncFixer02 warning when accessing task.Result after Task.WhenAll(tasks)? HOT 2
- Documenting Mapping Between AsyncFixer and VSThreading Analyzers HOT 1
- Code is sometimes mangled if it is a sync call that is supplying a method argument
- Parentheses are not added if a chained sync call is converted
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asyncfixer.