Skip to content

Instantly share code, notes, and snippets.

@bradwilson
Created April 23, 2015 17:15
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradwilson/05ffdad8c73cf9230261 to your computer and use it in GitHub Desktop.
Save bradwilson/05ffdad8c73cf9230261 to your computer and use it in GitHub Desktop.
Beware the cost of "async"
// Don't do this, because you don't need async if you're only calling one
// downstream async method and returning its value, unprocessed.
public async Task<int> DoSomething(int x, int y)
{
return await SlowMath.AddAsync(x, y);
}
// Do this instead:
public Task<int> DoSomething(int x, int y)
{
return SlowMath.AddAsync(x, y);
}
// However, don't take this shortcut if you are using try/catch, because the behavior
// is NOT the same (i.e., continue to use async here):
public async Task<int> DoSomething(int x, int y)
{
try
{
return await SlowMath.AddAsync(x, y);
}
catch(OverflowException ex)
{
Logger.Log(string.Format("Bummer, I couldn't add {0} and {1}", x, y));
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment