In this MSDN article, the following example code is provided (slightly edited for brevity):
public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Department department = await db.Departments.FindAsync(id);
if (department == null)
{
return HttpNotFound();
}
return View(department);
}
The FindAsync
method retrieves a Department
object by its ID, and returns a Task<Department>
. Then the department is immediately checked to see if it is null. As I understand it, asking for the Task's value in this manner will block code execution until the value from the awaited method is returned, effectively making this a synchronous call.
Why would you ever do this? Wouldn't it be simpler to just call the synchronous method Find(id)
, if you're going to immediately block anyway?