2

If I'm returning the result of a single asynchronous function e.g. in a service between application layers, is there any difference between:

    public Task<byte[]> GetInsurancePolicyDocument(string itineraryReference)
    {
        return this.coreItineraryDocumentService.GetInsurancePolicyDocument(itineraryReference);
    }

And:

    public async Task<byte[]> GetInsurancePolicyDocument(string itineraryReference)
    {
        return await this.coreItineraryDocumentService.GetInsurancePolicyDocument(itineraryReference);
    }
axle_h
  • 553
  • 1
  • 5
  • 16
  • Without more context, this answer is impossible to be answered. BTW, don't use async/await because it is cool. Use it when you do need it. – Lex Li May 30 '14 at 09:00
  • @LexLi, I disagree but its moot, this is a duplicate. – Jodrell May 30 '14 at 09:10
  • Clearly this function is doing I/O so I don't think I'm using async-await 'because it's cool'. The result of this and similar services eventually make their way to an MVC controller where they are awaited asynchronously (Task.WhenAll) before being stuck in a View-Model. – axle_h May 30 '14 at 09:15

1 Answers1

3

There are a few very subtle differences in some cases - for example, if the original Task returns with a status of Faulted but with an OperationCanceledException then the async version would return a task with a status of Canceled... but unless you really need any of that subtlety, it would generally be better to go with your first code and avoid the (very slight) overhead of wrapping it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194