C#C
C#14mo ago
Wrenpo

Safely getting a result from a async method within a sync method

I have an async method that returns a response object. In the calling sync method, what is the best way to get the result and capture any possible exceptions?

public static Task<ResponseObj> AsyncMethod()
{
  // ... does stuff
  return responseObj;
}

public void SyncMethod()
{
  // ... does stuff;
  // How to handle this safely?
  var response = AsyncMethod();
}
Was this page helpful?