Skip to content

Is AsyncFixer01 really an anti-pattern? #19

@Eli-Black-Work

Description

@Eli-Black-Work

Thanks for the analyzer! 🙂

We've been using the analyzer in several of our projects for a while now. We always disable the AsyncFixer01: "Unnecessary async/await usage" warning, because the fix elides methods from the stack trace when an exception is thrown.

For example:

async Task SaveItemAsync() {
   throw new Exception();
}

async Task UpdateAndSaveAsync() {
   //...
   await SaveItemAsync();
}

Main() {
   await UpdateAndSaveAsync();
}

In this case the stack track will show like this:

Main()
UpdateAndSaveAsync()
SaveItemAsync()

If we take the AsyncFixer01 suggestion and change the code to this:

async Task SaveItemAsync() {
   throw new Exception();
}

Task UpdateAndSaveAsync() {
   //...
   return SaveItemAsync();
}

Main() {
   await UpdateAndSaveAsync();
}

Then the stack track will not contain PrintAsync():

Main()
UpdateAndSaveAsync()

To me, this suggests that "unnecessarily" using await isn't an really an antipattern.

Would like to hear your thoughts. Thanks again for the analyzer 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions