115

What's the difference between doing the following:

async Task<T> method(){
    var r = await dynamodb.GetItemAsync(...)
    return r.Item;
}

vs

async Task<T> method(){
    var task = dynamodb.GetItemAsync(...)
    return task.Result.Item;
}

In my case, for some reason, only the second works. The first one never seems to end.

bubbleking
  • 3,329
  • 3
  • 29
  • 49
luis
  • 2,067
  • 2
  • 13
  • 21
  • So the only reason why I have found that a await could hang is from the below SO answers. I thought I would leave a comment here to help out future me. https://stackoverflow.com/questions/13489065/best-practice-to-call-configureawait-for-all-server-side-code https://stackoverflow.com/questions/9895048/async-call-with-await-in-httpclient-never-returns/10369275#10369275 – Cyrus Downey Jan 04 '18 at 16:20
  • 1
    Try await dynamodb.GetItemAsync(...).ConfigureAwait(false) – Jesper Mygind Aug 27 '18 at 19:46

2 Answers2

140

await asynchronously unwraps the result of your task, whereas just using Result would block until the task had completed.

See this explanantion from Jon Skeet.

Community
  • 1
  • 1
Frank Fajardo
  • 7,034
  • 1
  • 29
  • 47
  • 6
    why is await not working in this case though, but Result does work – luis Aug 27 '15 at 02:31
  • 14
    @luis: Lacking any other information, the only answer I see to that is that it's not actually working in the `await` case. You just mistakenly think it does because the method itself returns. But the task being awaited likely does not complete either way. If you want an answer to _that_ (which is a different question than the one you asked), you need to post a new question, stating that clearly, and providing [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) that reliably reproduces the problem. – Peter Duniho Aug 27 '15 at 02:35
  • 3
    Calling `Result` bare is a hidden deadlock. – Joshua Nov 26 '18 at 03:56
  • its a bit old post, but did you do `await Method()` ? – Luminous_Dev Feb 26 '20 at 01:54
  • @Joshua what do you mean with hidden deadlock? Deadlock as in multiple threads waiting on each other or do you mean a thread-blocking operation which counteracts async? – user2953241 Jun 18 '20 at 11:04
  • 2
    @user2953241: `.Result` or all of its possible variants cause a threadpool thread to wait for a job on the thread pool to finish. This can exhaust all threads on the threadpool leaving none to perform the actual work. – Joshua Jun 18 '20 at 13:02
  • I have the same issue as @luis, I'm retrieving data from SQL using dapper. When using async, after some executions, I'm getting a **Connection is closed error**. While, after replacing the await with `.Result`, works like a charm. Any explanation? – user3012488 Sep 09 '20 at 05:19
  • What's the point of using `await` instead of `.Result` in an asynchronous method B if the caller method A awaits the method B right away?, i.e. without doing any work between calling B and awaiting B. – Pedro Machado Oct 18 '22 at 11:13
  • @PedroMachado The point is that `.Result` causes the current thread to block and wait for the result. `await` allows the current thread to go off and be useful doing other work while the result is being produced. – Scott Langham Feb 20 '23 at 16:23
34

task.Result is accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method. Once the result of an operation is available, it is stored and is returned immediately on subsequent calls to the Result property. Note that, if an exception occurred during the operation of the task, or if the task has been cancelled, the Result property does not return a value. Instead, attempting to access the property value throws an AggregateException exception. The only difference is that the await will not block. Instead, it will asynchronously wait for the Task to complete and then resume

NASSER
  • 5,900
  • 7
  • 38
  • 57
  • 7
    why is await not working in this case though, but Result does work – luis Aug 27 '15 at 02:31
  • 5
    Calling `Result` bare is a hidden deadlock. – Joshua Nov 26 '18 at 03:56
  • 2
    Piggybacking off of Joshua, you can find out more about why it causes a hidden deadlock here: https://stackoverflow.com/questions/17248680/await-works-but-calling-task-result-hangs-deadlocks – Onosa Oct 15 '19 at 14:03