Beware the cost of "async"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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