.NET 6 adds a new WaitAsync method to System.Threading.Tasks.Task.
The documentation says the following about this method:
Gets a Task that will complete when this Task completes, when the specified timeout expires, or when the specified CancellationToken has cancellation requested.
So when is this alternative to Task.Wait useful?
There are a lot of libraries that provide task-based methods but don’t allow to pass a cancellation token. With Task.WaitAsync() you can add cancellation or timeout ability for async methods, that inherently don't provide such capability. Before you would typically write a helper method to solve this issue, but now you get a solution out-of-the-box.
An example
Here is an example where we add timeout functionality to an existing async method:
In this case the task never completes, so after 5 seconds (the configured timeout) a timeout exception will be thrown.
Another example would be to add cancellation support to an existing async method:
In this case the task will throw a TaskCanceledException after 5 seconds.