异步方法中的 Await 与 Task.Result [英] Await vs Task.Result in an Async Method
本文介绍了异步方法中的 Await 与 Task.Result的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
执行以下操作有什么区别:
What's the difference between doing the following:
async Task<T> method(){
var r = await dynamodb.GetItemAsync(...)
return r.Item;
}
对比
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.
推荐答案
await
异步解包任务的结果,而仅使用 Result 会阻塞直到任务完成.
await
asynchronously unwraps the result of your task, whereas just using Result would block until the task had completed.
这篇关于异步方法中的 Await 与 Task.Result的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文