Skip to content

Commit ea8459a

Browse files
CopilotBillWagner
andauthored
Clarify Task.WhenAny documentation to address user confusion about Task<Task> return type (#48051)
* Initial plan * Clarify Task.WhenAny documentation to address user confusion Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]>
1 parent 27a54b7 commit ea8459a

File tree

1 file changed

+1
-1
lines changed
  • docs/csharp/asynchronous-programming

1 file changed

+1
-1
lines changed

docs/csharp/asynchronous-programming/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ while (breakfastTasks.Count > 0)
249249
}
250250
```
251251

252-
Near the end of the code snippet, notice the `await finishedTask;` expression. The `await Task.WhenAny` expression doesn't wait on the finished task, but rather waits on the `Task` object returned by the `Task.WhenAny` method. The result of the `Task.WhenAny` method is the completed (or faulted) task. The best practice is to wait on the task again, even when you know the task is complete. In this manner, you can retrieve the task result, or ensure any exception that causes the task to fault is thrown.
252+
Near the end of the code snippet, notice the `await finishedTask;` expression. This line is important because `Task.WhenAny` returns a `Task<Task>` - a wrapper task that contains the completed task. When you `await Task.WhenAny`, you're waiting for the wrapper task to complete, and the result is the actual task that finished first. However, to retrieve that task's result or ensure any exceptions are properly thrown, you must `await` the completed task itself (stored in `finishedTask`). Even though you know the task has finished, awaiting it again allows you to access its result or handle any exceptions that might have caused it to fault.
253253

254254
### Review final code
255255

0 commit comments

Comments
 (0)