Skip to content

Redundant await expressions

Sergey Teplyakov edited this page May 26, 2016 · 1 revision

Asynchronous programming is much easier with high level language constructs like async/await. Unfortunately this awesome feature could be overused. One good example is using async/await when they're redundant.

Consider following example:

public async Task<int> DoSomethingAsync() {
//                     ~~~~~~~~~~~~~~~~
// Async/Await is redundant because all exit points are awaitable.
  return await DoSomethingForReal();
}

private async Task<int> DoSomethingForReal() {
  await Task.Delay(42);
  return 42;
}

Apparently, in method DoSomethingAsync could be simplified by removing async specifier from method declaration and changing return statement to return DoSomethingForReal().

Error prone detects such cases and shows warning and provides a code fixer that will remove await expression if all return statements are awaitable (and this is only await specifiers used in the method).

Clone this wiki locally