Olivier Albertini
Olivier Albertini

Reputation: 694

async await or just TasK<T>

I use example form asp.net here

so, i ask me, what the difference between 2 codes :

public class ServiceTest
{
    public Task<List<Widget>> WidgetsAsync(CancellationToken cancellationToken = default(CancellationToken))
    {
        var widgetService = new WidgetService();
        return widgetService.GetWidgetsAsync(cancellationToken);
    }

    public Task<List<Product>> ProductAsync(CancellationToken cancellationToken = default(CancellationToken))
    {
        var prodService = new ProductService();
        return prodService.GetProductsAsync(cancellationToken);
    }

    public Task<List<Gizmo>> GizmoAsync(CancellationToken cancellationToken = default(CancellationToken))
    {
        var gizmoService = new GizmoService();
        return gizmoService.GetGizmosAsync(cancellationToken);
    }
}

and

public class ServiceTest
{
    public async Task<List<Widget>> WidgetsAsync(CancellationToken cancellationToken = default(CancellationToken))
    {
        var widgetService = new WidgetService();
        return await widgetService.GetWidgetsAsync(cancellationToken);
    }

    public async Task<List<Product>> ProductAsync(CancellationToken cancellationToken = default(CancellationToken))
    {
        var prodService = new ProductService();
        return await prodService.GetProductsAsync(cancellationToken);
    }

    public async Task<List<Gizmo>> GizmoAsync(CancellationToken cancellationToken = default(CancellationToken))
    {
        var gizmoService = new GizmoService();
        return await gizmoService.GetGizmosAsync(cancellationToken);
    }
}

Elapsed time is exactly the same for me.. I'm beginning with async and so maybe it's a stupid question but i wanted to be sure before taking a bad direction :)

Upvotes: 1

Views: 183

Answers (2)

Medo42
Medo42

Reputation: 3821

Often, a function will use the awaited value for some further calculations (and perhaps even start new async tasks with it and await them in turn...) before returning a final result. The C# compiler will then rewrite the function to return a Task which yields the final result, once it is available.

In your case the final result is the same as that of the async operation that you call, so you can just as well return that task directly. However, as Cory points out in his answer, there is a difference if an exception is thrown in your functions - with an async function, this will be wrapped into the resulting task, otherwise it will propagate normally and will have to be handled by the caller.

Upvotes: 2

Cory Nelson
Cory Nelson

Reputation: 30021

There is one small but major behavioral difference between the two: an exception thrown inside of an async method will be wrapped in a Task. In your non-async methods, it'll just be thrown on invocation and no Task will be returned.

A lot of code using Tasks is not expecting an exception to be thrown on invocation. I'd recommend not doing this unless you are really sure it won't throw.

Upvotes: 5

Related Questions