Skip to content

Files

Latest commit

 

History

History
9 lines (5 loc) · 752 Bytes

PH_B012.md

File metadata and controls

9 lines (5 loc) · 752 Bytes

PH_B012 - Multiple Awaits on the Same ValueTask

Problem

The returned ValueTask of a method invocation is awaited multiple times. Awaiting the same ValueTask can be problematic since the class behind the invoked method may recycle the underlying task. Therefore, a ValueTask does not have the guarantee, as Task does, that it never transitions away from the completed state.

Solution

Make sure to await the returned ValueTask only once. However, it is best practice to await the method invocation rather than storing the returned ValueTask in a local variable. If both options are not possible, transform the ValueTask into a conventional Task with the help of the AsTask method (which may only be used once as well).