Giter Club home page Giter Club logo

Comments (4)

jannesrsa avatar jannesrsa commented on September 2, 2024 10

Here is guidance from David Fowler.

https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#prefer-asyncawait-over-directly-returning-task

Prefer async/await over directly returning Task

There are benefits to using the async/await keyword instead of directly returning the Task:

  • 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.

Eli-Black-Work avatar Eli-Black-Work commented on September 2, 2024 2

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.

bstordrup avatar bstordrup commented on September 2, 2024 1

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:

  1. Do not elide by default. Use the async and await for natural, easy-to-read code.
  2. 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.

RudolfJan avatar RudolfJan commented on September 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.