Skip to content

Improve "Async return types" docs #47289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2025
Merged

Improve "Async return types" docs #47289

merged 2 commits into from
Jul 17, 2025

Conversation

xakep139
Copy link
Contributor

@xakep139 xakep139 commented Jul 11, 2025

Summary

This PR rephrases a misleading statement regarding an awaiter type - an AsyncMethodBuilderAttribute should be applied on a type that an async method returns, not on a type that GetAwaiter() method returns.

The same requirement is showcased in the spec - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#15141-general.

As an example:

async CustomTask SomeMethodAsync() { /* ... */ }

// The attribute should be applied here:
[AsyncMethodBuilder(typeof(CustomAsyncMethodBuilder))]
class CustomTask
{
    public CustomAwaiter GetAwaiter() { /* ... */ }
}

class CustomAwaiter : INotifyCompletion
{
    public void OnCompleted(Action continuation) { /* ... */ }
}

internal class CustomAsyncMethodBuilder
{
    // The builder must implement a bunch of methods like (only a few required are listed):
    public static CustomAsyncMethodBuilder Create() { /* ... */ }
    public void SetException(Exception exception) { /* ... */ }
    public void SetResult() { /* ... */ }
    public CustomTask Task { get; }
}

Rigth now the documentation states that an AsyncMethodBuilderAttribute should be applied on a CustomAwaiter type from the example above, which is misleading.


Internal previews

📄 File 🔗 Preview link
docs/csharp/asynchronous-programming/async-return-types.md Async return types (C#)

@xakep139 xakep139 requested review from BillWagner and a team as code owners July 11, 2025 08:38
@dotnetrepoman dotnetrepoman bot added this to the July 2025 milestone Jul 11, 2025
@dotnet-policy-service dotnet-policy-service bot added dotnet-csharp/svc async-task-programming/subsvc community-contribution Indicates PR is created by someone from the .NET community. labels Jul 11, 2025
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @xakep139

Thanks for spotting this. However, the fix isn't correct either. I looked back at the spec and made a suggestion for different text.

Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, this LGTM

@BillWagner BillWagner enabled auto-merge (squash) July 17, 2025 15:30
@BillWagner BillWagner merged commit 2d6f0fe into dotnet:main Jul 17, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async-task-programming/subsvc community-contribution Indicates PR is created by someone from the .NET community. dotnet-csharp/svc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants