// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace System.Threading.Tasks
{
/// Provides a set of static methods for working with specific kinds of instances.
public static class TaskExtensions
{
/// Creates a proxy that represents the asynchronous operation of a .
/// The to unwrap.
/// A that represents the asynchronous operation of the provided .
public static Task Unwrap(this Task task)
{
if (task == null)
{
throw new ArgumentNullException(nameof(task));
}
// If the task hasn't completed or was faulted/canceled, wrap it in an unwrap promise. Otherwise,
// it completed successfully. Return its inner task to avoid unnecessary wrapping, or if the inner
// task is null, return a canceled task to match the same semantics as CreateUnwrapPromise.
return
!task.IsCompletedSuccessfully ? Task.CreateUnwrapPromise(task, lookForOce: false) :
task.Result ??
Task.FromCanceled(new CancellationToken(true));
}
/// Creates a proxy that represents the asynchronous operation of a .
/// The to unwrap.
/// A that represents the asynchronous operation of the provided .
public static Task Unwrap(this Task> task)
{
if (task == null)
{
throw new ArgumentNullException(nameof(task));
}
// If the task hasn't completed or was faulted/canceled, wrap it in an unwrap promise. Otherwise,
// it completed successfully. Return its inner task to avoid unnecessary wrapping, or if the inner
// task is null, return a canceled task to match the same semantics as CreateUnwrapPromise.
return
!task.IsCompletedSuccessfully ? Task.CreateUnwrapPromise(task, lookForOce: false) :
task.Result ??
Task.FromCanceled(new CancellationToken(true));
}
}
}